首页 > 解决方案 > 如何使用 Amplify 或 RNFetchBlob 将视频上传到 S3?

问题描述

我正在使用react-native-image-picker在我的 iOS 模拟器手机上为我的 react 本机应用程序获取视频的文件路径。我如何使用它通过 Amplify 上传到 S3?

import ImagePicker from 'react-native-image-picker';
import RNFetchBlob from 'react-native-fetch-blob';

class App extends Component {

 constructor(props) {
    super(props)

   this.state = {
     vidFileName: '',
     fileURI: '',
   }

    this.putFileInS3 = this.putFileInS3.bind(this);  
 }

  pickVideo = async () => {

    const options = {
       mediaType: 'video'
      };
      ImagePicker.launchImageLibrary(options, (response) => {
          console.log('Response = ', response);

         this.setState({ 
            vidFileName: response.fileName,
            fileURI: response.uri,
          });

         this.putFileInS3();
     }
  }

 readFile = (somefilePath) => {
     return RNFetchBlob.fs.readFile(somefilePath, 'base64').then(data => new 
      Buffer(data, 'base64'));
    } 


putFileInS3 = async () => 
  {
     RNFetchBlob.fs.readFile(this.fileURI).then(buffer => {
       Storage.put(vidFileName, buffer, { contentType: 'video/mp4' })
       conole.log('successfully uploaded video to bucket');
     }).catch(e => {
       console.log(e);
     });

  }

我正在尝试遵循:https://aws-amplify.github.io/docs/js/storage,在 React Native 应用程序中上传图像部分。但也许我错过了什么?你能帮忙吗?

Error: file:///Users/xxxxxxxxxx/Library/Developer/CoreSimulator/Devices/xxxxxxxxx/data/Containers/Data/Application/5CxxxxxxxxxxxxxC79/tmp/4xxxxxxxxxxxxxx.MOV . <-URI
App.js:130 Error: file not exists
    at createErrorFromErrorData (NativeModules.js:146)
    at NativeModules.js:95
    at MessageQueue.__invokeCallback (MessageQueue.js:397)
    at MessageQueue.js:127
    at MessageQueue.__guard (MessageQueue.js:297)
    at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126)
    at debuggerWorker.js:72

标签: reactjsamazon-web-servicesreact-nativeaws-amplify

解决方案


这个包非常适合将数据发送到 S3 中的存储桶react-native-aws3

如果您在使用该软件包时需要任何帮助,请发表评论。


推荐阅读