question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[CRITICAL] [lib-storage] Uploading in Safari is in a non-working state

See original GitHub issue

Describe the bug

When uploading from Safari directly to S3 (credentials supplied via Cognito), I get this error:

Body Data is unsupported format, expected data to be one of: string | Uint8Array | Buffer | Readable | ReadableStream | Blob

I’m passing a File object like so:

const upload = new Upload({
  client: s3,
  params: {
    Bucket: process.env.VUE_APP_UPLOAD_BUCKET,
    Body: file,
    ContentType: file.type,
    Key: destKey,
    Metadata: metadata,
  }
});

Your environment

SDK version number

@aws-sdk/lib-storage@3.14.0

Is the issue in the browser/Node.js/ReactNative?

Browser

Details of the browser/Node.js/ReactNative version

Safari 14.0.2 and under

Steps to reproduce

(see code snippet above)

Observed behavior

Uploading a file using the Upload class fails with error: Body Data is unsupported format, expected data to be one of: string | Uint8Array | Buffer | Readable | ReadableStream | Blob

Expected behavior

I would expect Webkit/Safari to follow W3C standards better. 😉 Also, uploads worked totally fine in SDK v2.

Additional context

Pour yourself a coffee and pull up a chair…

So, lib-storage is checking for the existence of data.stream as a function, which is totally legit:

https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-storage/src/chunker.ts#L19-L21

File, inheriting from Blob, should implement that. Unfortunately, in Safari, Blob.prototype.stream() does not exist, even though this is a W3C standard drafted way back in May 2019. This wasn’t supported in Safari until 14.1.

I’m working around this with the following code, but it’s not ideal, as it creates a slight lag for the user as very large files (potentially 70-150MB) are converted to Uint8Array before passing to the Upload constructor:

const fileBuffer = await file.arrayBuffer();
const uploadParams: PutObjectCommandInput = {
  Bucket: process.env.VUE_APP_UPLOAD_BUCKET,
  Body: new Uint8Array(fileBuffer),
  ContentType: file.type,
  Key: destKey,
  Metadata: metadata,
};
const upload = new AWSUpload({
  client: s3,
  params: uploadParams,
});

I don’t think there’s any ideal workaround at this point. But maybe if this library could handle this scenario instead of putting that on the developer (so they don’t wind up wasting time like I did), that would be helpful. Maybe the getChunk function could check for data being an instance of Blob but with data.stream returning undefined, and if so, then call data.arrayBuffer() to parse the data and return it.

If there’s a better way I can handle this, I’m open to hearing about it! But if Safari doesn’t support Blob streaming, it seems like reading the entire file into memory is the only alternative at this point.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:29 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
ludwighencommented, Sep 22, 2021

@ffxsam I have the same issue with safari crashing and reloading the web page. We’re however using a React app.

2reactions
ajredniwjacommented, May 25, 2021

I understand, let me try and work with the example you provided.

Read more comments on GitHub >

github_iconTop Results From Across the Web

If Safari on Mac doesn't open a webpage or isn't working as ...
Reload the page. To reload a page, choose View > Reload Page, or press Command-R. · Check Safari extensions · Check Safari settings...
Read more >
I cannot upload files with Safari after macOS 12.0.1 update
I have tried activating the Develop window in Safari and ticking "Disable Local File Restrictions" but this hasn't changed anything. Any advice ...
Read more >
Change Websites settings in Safari on Mac - Apple Support
In Safari on your Mac, customize how you work with an individual website. ... To change these settings, choose Safari > Settings, then...
Read more >
App Review - App Store - Apple Developer
Expedited reviews. You can request the review of your app to be expedited if you face extenuating circumstances, such as fixing a critical...
Read more >
Apple security updates
Name and information link Available for Release date iCloud for Windows 14.1 Windows 10 and later via the Microsoft Store 13 Dec 2022 Safari 16.2...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found