繁体   English   中英

在React Native(Expo)中将图像转换为Blob并上传到S3存储桶

[英]Turning image into blob in React Native (Expo) and uploading to S3 bucket

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

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

我的下一步是将本地cacheDirectory中的图像转换为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

          }

但是,为完成该过程中的这一小步骤而安装的任何方法或模块根本没有起作用。 由于expo客户端,我无法使用RNFSreact-native-fetch-blob或任何其他需要链接的模块。 我不想只为了一件事而放弃博览会。 还有其他方法可以做到这一点吗?

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

};

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM