Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native Expo Error [Error: TypeError: stream.arrayBuffer is not a function (it is undefined)] #6733

Open
3 of 4 tasks
tomatenketchup opened this issue Dec 14, 2024 · 3 comments
Assignees
Labels
bug This issue is a bug. p2 This is a standard priority issue

Comments

@tomatenketchup
Copy link

Checkboxes for prior research

Describe the bug

I'm getting an error that says, "[Error: TypeError: stream.arrayBuffer is not a function (it is undefined)] " although the files are uploaded to the bucket

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/client-s3@^3.712.0, .^3.701.0

Which JavaScript Runtime is this issue in?

React Native

Details of the browser/Node.js/ReactNative version

0.76.2

Reproduction Steps

export const uploadFileToR2 = async (file: any) => {
  try {
    // move these to cloud or backend or cloud functions
    const s3Client = new S3Client({
      region: 'auto',
      endpoint: `https://${config.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
      credentials: {
        accessKeyId: config.CLOUDFLARE_ACCESS_KEY,
        secretAccessKey: config.CLOUDFLARE_SECRET_KEY
      }
    })

    const { uri, mimeType } = file

    // get file from uri
    const uriResponse = await fetch(uri)
    const contentBody = await uriResponse.blob()

    // get file extension
    const extension = mime.getExtension(mimeType)

    // create random file name
    const fileName = `${Date.now()}-${Math.random()
      .toString()
      .replace('.', '-')}.${extension}`

    const putObjectCommand = new PutObjectCommand({
      Bucket: config.CLOUDFLARE_BUCKET_NAME,
      Key: fileName,
      Body: contentBody,
      ContentType: mimeType
    })

    const response = await s3Client.send(putObjectCommand)
    return response
  } catch (error: any) {
    console.log(error?.$response)
    throw new Error(error)
  }
}

Observed Behavior

(NOBRIDGE) LOG  [Error: TypeError: stream.arrayBuffer is not a function (it is undefined)

Expected Behavior

The expected behavior shouldn't throw an error on the catch since the files were uploaded the to bucket successfully

Possible Solution

No response

Additional Information/Context

The tricky part is, the files are uploaded to the bucket. I'm using Cloudflare R2. I even tried older version of the package, still, the error persists. I even tried using axios for getting the blob, still, the error persists.

@tomatenketchup tomatenketchup added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 14, 2024
@Adelkkaa
Copy link

same issue

@aBurmeseDev aBurmeseDev self-assigned this Dec 17, 2024
@aBurmeseDev
Copy link
Member

Hi @tomatenketchup - thanks for reaching out.

This error occurs because SDK expects the Body param of the PutObject to be a readable stream or a typed array like Uint8Array. However, you're passing a Blob object directly, which doesn't have an arrayBuffer method.

You need to convert the Blob to a Uint8Array or a readable stream before passing it to the PutObjectCommand. Here's modified code:

  ....

   // create random file name
    const fileName = `${Date.now()}-${Math.random()
      .toString()
      .replace('.', '-')}.${extension}`

    // Convert Blob to Uint8Array
    const arrayBuffer = await contentBody.arrayBuffer()
    const buffer = new Uint8Array(arrayBuffer)

    const putObjectCommand = new PutObjectCommand({
      Bucket: config.CLOUDFLARE_BUCKET_NAME,
      Key: fileName,
      Body: buffer, // Pass the Uint8Array as the Body
      ContentType: mimeType
    })
  ....

Hope that helps!
John

cc: @Adelkkaa

@aBurmeseDev aBurmeseDev added response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Dec 18, 2024
@ManiMohan-Radhakrishnan
Copy link

ManiMohan-Radhakrishnan commented Dec 19, 2024

same issue

const bodyBuffer = Buffer.from(data, 'utf-8');
const checksum = await sha256(bodyBuffer.toString('utf-8'));
const checksumCreateTime = new Date().getTime();

const wsClient = WebSocketClient.getInstance();

const params = {
Bucket: bucketName,
Key: objectKey,
Body: bodyBuffer,
ContentType: contentType,
};

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

4 participants