【问题标题】:Image uploaded incorrectly to s3 using amplify Storage (React Native - Expo - Amplify)图片使用放大存储错误上传到 s3 (React Native - Expo - Amplify)
【发布时间】:2019-04-01 14:41:07
【问题描述】:
  • 我正在尝试使用 Amplify Storage API 将图像上传到 s3。
  • 我使用 EXPO imagePicker 将图片从手机上传到 React-Native,并且显示正确。
  • 然后我使用放大 Storage.put 将它上传到 s3,它到达 s3 中的正确文件夹,具有我给它的正确文件名和我提供的公共访问权限(对图像和存储桶本身)。李>
  • 但是如果我尝试在 s3 中打开照片 uri,它不会显示。当我检查浏览器时,它在控制台上显示此错误:

  • 如果我在浏览器中粘贴从 Storage.get 获得的 uri,我会收到以下错误:

  _handleImagePicked = async (pickerResult) => {
    const s3path = 'fotos_cdcc/' + '186620' + '/'
    const imageName = '186620_4' + '.jpeg'
    const key = s3path + imageName
    const fileType = pickerResult.type;
    const access = { level: "public", contentType: 'image/jpg' };
    const imageData = await global.fetch(pickerResult.uri)
    const blobData = await imageData.blob()

    try {
      await Storage.put(
        key, 
        blobData, 
        access,
        fileType
        ).then(result => console.log(result))
    } catch (err) {
      console.log('error: ', err)
    }
  }
  • pickerResult 具有这种形式:
Object {
"cancelled": false,
"height": 750,
"type": "image",
"uri": "file:///var/mobile/Containers/Data/Application/021D6288-9E88-4080-8FBF-49F5195C2073/Library/Caches/ExponentExperienceData/%2540anonymous%252Fcaballos-app-dd2fa92e-4625-47e7-940e-1630e824347a/ImagePicker/D9EE1F24-D840-457F-B884-D208FFA56892.jpg",
"width": 748,
}
  • 我尝试在 s3 中公开存储桶和照片,但错误仍然存​​在。

有什么想法吗?

提前致谢!

【问题讨论】:

  • 您在 amazon s3 控制台中看到文件了吗?
  • @Cherniv 是的,我愿意。更重要的是,我将图像直接从我的计算机上传到同一个 s3 文件夹(显示正确的图像),它们与 Storage.put 上传的有冲突的图像似乎没有区别
  • 当我看到错误时,可能是 awsserver 访问问题。检查套接字或访问权限。
  • @hongdevelop 我公开了所有内容(存储桶、图像),并且 iam 用户拥有完全权限,所以我认为访问权限不是问题。

标签: image react-native amazon-s3 expo aws-amplify


【解决方案1】:

也许这不是 aws-amplify 的问题,而是 fetchblob。你可以试试这个吗?

function urlToBlob(url) {
  return new Promise((resolve, reject) => {
      var xhr = new XMLHttpRequest();
      xhr.onerror = reject;
      xhr.onreadystatechange = () => {
          if (xhr.readyState === 4) {
              resolve(xhr.response);
          }
      };
      xhr.open('GET', url);
      xhr.responseType = 'blob'; // convert type
      xhr.send();
  })
}

然后代替

const imageData = await global.fetch(pickerResult.uri)
const blobData = await imageData.blob()

试试

const blobData = await urlToBlob(pickerResult.uri)

请查看完整讨论https://github.com/expo/firebase-storage-upload-example/issues/13

【讨论】:

  • 我们让它与另一种解决方案一起使用,但在某些手机中上传仍然存在问题。只需将其更改为您的解决方案即可!谢谢!
  • 感谢您的提示!
猜你喜欢
  • 2020-03-26
  • 2015-08-24
  • 2019-07-17
  • 2021-11-30
  • 1970-01-01
  • 2019-07-25
  • 2018-11-28
  • 2020-03-30
  • 2020-09-16
相关资源
最近更新 更多