itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.89k stars 954 forks source link

Uploading file to s3 is always setting binary content type #1202

Open Moussawi7 opened 7 months ago

Moussawi7 commented 7 months ago

Whenever I attempt to upload files to S3 using a presigned URL, I consistently encounter an issue where the correct content type is not being set, and instead, it consistently defaults to the binary type. What might be the underlying cause of this problem?

      const {path} = await RNFS.stat(fileURI);
      const files = [
        {
          name: 'file',
          filename: fileName,
          filepath: path,
          filetype: fileType,
          type: fileType,
        },
      ];

      const fields = {
        Key: params.key,
        Bucket: params[''],
        Policy: params.policy,
        AWSAccessKeyId: params.AWSAccessKeyId,
        Signature: params.signature,
      };

      const response = await RNFS.uploadFiles({
        toUrl: url,
        files,
        method: 'POST',
        headers: {
          Accept: 'application/json',
        },
        fields,
      }).promise;
      console.log(response);
      return response;
    } catch (exception) {
      Logger.warn('Unable to upload to s3', exception);
      return null;
    }
  }
saketh-tadimeti commented 6 months ago

hey @Moussawi7 can you try setting the Content-Type header ?

Moussawi7 commented 4 months ago

Hey @saketh-tadimeti , I get the following in this case

{"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Error><Code>PreconditionFailed</Code><Message>At least one of the pre-conditions you specified did not hold</Message><Condition>Bucket POST must be of the enclosure-type multipart/form-data</Condition><RequestId>06NNA81VENJQ1K0Z</RequestId><HostId>DMgE0rgYtkyZvu0WuWHwCfPASTwdtrIYWMV3udb2DeKoo59+E8C6mIChZ5tGz4UAil1NpQS/mLE=</HostId></Error>", "headers": {"Connection": "close", "Content-Type": "application/xml", "Date": "Wed, 20 Dec 2023 14:47:18 GMT", "Server": "AmazonS3", "Transfer-Encoding": "Identity", "x-amz-id-2": "DMgE0rgYtkyZvu0WuWHwCfPASTwdtrIYWMV3udb2DeKoo59+E8C6mIChZ5tGz4UAil1NpQS/mLE=", "x-amz-request-id": "06NNA81VENJQ1K0Z"}, "jobId": 1, "statusCode": 412}