【问题标题】:Turning image into blob in React Native (Expo) and uploading to S3 bucket在 React Native (Expo) 中将图像转换为 blob 并上传到 S3 存储桶
【发布时间】:2019-07-25 05:37:58
【问题描述】:

我有一个点击图片的相机组件。我使用 expo 的 FileSystem 将点击的图片存储在本地 cacheDirectory 中。看起来像这样:

onPictureSaved = async photo => {
    await FileSystem.moveAsync({
        from: photo.uri,
        to: `${FileSystem.cacheDirectory}test.jpg`
    });}

我的下一步是我的下一站是将本地缓存目录中的图像转换为 blob 并通过aws-sdk 将图像上传到 S3:

var params = {
            Bucket: "my-bucket", 
            Key: 'test.jpg',
            Body: blob
           };

           s3.upload(params, function(err, data) {
            if (err) {
                console.log(err);
            } // an error occurred
            else  { 
                console.log(data);
            }         // successful response

          }

但是,我安装的任何方法或模块以完成此过程中的这一微小步骤,但根本不起作用。由于世博会客户端,我不能使用RNFS、react-native-fetch-blob 或任何其他需要链接的模块。我不想仅仅为了一件事而分离世博会。有没有其他方法可以做到这一点?

【问题讨论】:

    标签: javascript amazon-web-services react-native amazon-s3 expo


    【解决方案1】:

    看看https://github.com/expo/image-upload-example/issues/3#issuecomment-387263080。最新的 expo 版本支持 blob,因此您可以执行以下操作:

    uploadToS3 = async (fileUri, s3Bucket, s3Key) => {
    const response = await fetch(fileUri);
    const blob = await response.blob();
    return new Promise((resolve, reject) => {
      const params = {
        Bucket: s3Bucket,
        Key: s3Key,
        Body: blob,
      };
      s3.upload(params, function(err, data) {
        if (err) {
          console.log('Something went wrong');
          console.log(err);
          reject(err);
        } else {
          console.log('Successfully uploaded image');
          resolve(data);
        }
      });
    });
    

    };

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-06-14
      • 2019-08-17
      • 1970-01-01
      • 2018-11-28
      • 2019-10-18
      • 1970-01-01
      • 2020-12-30
      • 2018-03-11
      • 2020-02-14
      相关资源
      最近更新 更多