Callback support for FileSystem.uploadAsync
Sindre
Checked the source code and found that UploadTask and createUploadTask has been available since August 2021. It's not documented in the docs, but it has TS types so it's quite easy to use.
Here's the PR that introduces it, including example code:
https://github.com/expo/expo/pull/13318
Richard Barbour
Sindre: Thanks for pointing this out. This was indeed very easy to implement.
Probably something that should be in the docs as very useful.
Yassin Eldeeb
any updates 😕
Irving Emmanuel Gonzalez
Hey Richard, could you explain how to upload the file to S3 using "FileSystem.uploadAsync"? TIA
Richard Barbour
Irving Emmanuel Gonzalez: I had previously used Axios to do this, which did allow me to display progress, but swapped to FileSystem.uploadAsync to support uploading in the background / handle the retry more elegantly. This turned out to be relatively straight forward actually:
- I call my backend (which is a Rails API) to generate and return a S3 presigned URL for upload using the S3 sdk, see example below and here for more details.
s3_bucket = Aws::S3::Resource.new(
region: ENV['AWS_REGION'],
credentials: Aws::Credentials.new(
ENV['AWS_ACCESS_KEY_ID'],
ENV['AWS_SECRET_ACCESS_KEY']
)
).bucket(ENV['S3_BUCKET_NAME'])
presigned_url = s3_bucket.presigned_post(
key: "YOURS3KEY",
acl: 'private',
success_action_status: '201'
)
return {
presigned_url: presigned_url.url,
url_fields: presigned_url.fields
}
- Using this presigned URL, I can securely upload from my Expo app something like using:
FileSystem.uploadAsync(presignedUrl, fileUri, {
uploadType: FileSystemUploadType.MULTIPART,
fieldName: "file",
parameters: urlFields
})