react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

Uploading file to s3 is always setting binary content type

Open Moussawi7 opened this issue 1 year ago • 2 comments

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;
    }
  }

Moussawi7 avatar Sep 20 '23 08:09 Moussawi7

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

saketh-tadimeti avatar Nov 11 '23 04:11 saketh-tadimeti

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}

Moussawi7 avatar Dec 20 '23 14:12 Moussawi7