aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
2.98k stars 560 forks source link

can't upload file to s3 in react-native #5978

Closed lukerlv closed 3 months ago

lukerlv commented 3 months ago

Checkboxes for prior research

Describe the bug

Previously, it was possible to upload, but now it is no longer possible. The upload interface returned {"data": ""}, and the error message from react-native is as follows:

// node_modules\whatwg-fetch\dist\fetch.umd.js
xhr.onerror = function(e) {
  setTimeout(function() {
    reject(new TypeError('Network request failed'));
  }, 0);
};

SDK version number

@aws-sdk/client-s3@3.550.0, @aws-sdk/lib-storage@3.550.0

Which JavaScript Runtime is this issue in?

React Native

Details of the browser/Node.js/ReactNative version

0.72.7

Reproduction Steps

Previously, it was possible to upload, but now it is no longer possible. The upload interface returned {"data": ""}, and the error message from react-native is as follows:

// node_modules\whatwg-fetch\dist\fetch.umd.js
xhr.onerror = function(e) {
  setTimeout(function() {
    reject(new TypeError('Network request failed'));
  }, 0);
};

my upload code is as follows:

import 'react-native-get-random-values';
import 'react-native-url-polyfill/auto';
import {S3Client, PutObjectCommand} from '@aws-sdk/client-s3';
import {Upload} from '@aws-sdk/lib-storage';

const upload = async (videoRes: IImage) => {
    const res: any = await tokenInfoApi({});
    const {accessKeyId, accessKeySecret, expireTime, token, bucketName} = res.model;

    const createParams = {
      Bucket: bucketName,
      Key: filePath.current,
    };

    const credentials = {
      accessKeyId: accessKeyId,
      secretAccessKey: accessKeySecret,
      sessionToken: token,
    };

    const client = new S3Client({
      region: regionCode,
      credentials: credentials,
    });

    const _source = Platform.OS === 'android' ? videoRes.path : 'file://' + videoRes.path;
    const response = await fetch(_source);
    const blob = await response.blob();

    const parallelUploads3 = new Upload({
        client: client,
        params: {
          Bucket: createParams.Bucket,
          Key: createParams.Key,
          Body: blob,
        },
        tags: [
          /*...*/
        ], // optional tags
        queueSize: 4, // optional concurrency configuration
        partSize: 1024 * 1024 * 10,// optional size of each part, in bytes, at least 5MB
        leavePartsOnError: true, // optional manually handle dropped parts
      });

      parallelUploads3.on('httpUploadProgress', (progress: any) => {

      });

      await parallelUploads3.done();

      uploadSuccess && uploadSuccess(filePath.current, coverUrl)
  };

Observed Behavior

Previously, it was possible to upload, but now it is no longer possible. The upload interface returned {"data": ""}, and the error message from react-native is as follows:

// node_modules\whatwg-fetch\dist\fetch.umd.js
xhr.onerror = function(e) {
  setTimeout(function() {
    reject(new TypeError('Network request failed'));
  }, 0);
};

Expected Behavior

upload success

Possible Solution

No response

Additional Information/Context

No response

RanVaknin commented 3 months ago

Hi @lukerlv ,

Thanks for reaching out. Can you please provide the stack trace of the full error?

Additionally you mentioned

Previously, it was possible to upload, but now it is no longer possible.

Did you update the SDK to a version that stopped working? You didn't mention if this was after updating your SDK, what was the previous state, and the current one.

Thanks, Ran~

lukerlv commented 3 months ago

Hi @RanVaknin ,

Thanks for your help.

My previous version is 3.490.0,but it also has this problem: (Previously, it was possible to upload, but now it is no longer possible.) Sorry, I don't know how to provide the full error, but the error occured is as follows: image My upload request is as follows: image image

Thanks, luker~

lukerlv commented 3 months ago

Hi @RanVaknin, I have discovered the reason for this issue, which is due to the issue with my office's WiFi. I cannot upload using WiFi, but I can upload using my phone's mobile network. I'm sorry for causing you trouble, but I can't figure out why my office's WiFi cannot upload files to S3. Is there anywhere to configure it?

Thanks for your help. luker~

RanVaknin commented 3 months ago

Hi @lukerlv ,

Happy to hear that you were able to diagnose the issue. There are no SDK configurations for network. Establishing a network connection is a lower level networking event that happens on the OS level. My only thought is if you have a firewall setup you might want to check its configuration to see if you can allow more traffic out.

Thanks, Ran~

lukerlv commented 3 months ago

Hi @RanVaknin ,

The network issue may require my colleagues to assist in handling it. If the reason is found, I will update here. Thank you for your help!

Thanks, luker~

github-actions[bot] commented 2 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.