Reputation: 443
I'm using react-native-image-picker
to get a file path of a video on my iOS simulator phone for my react native app. How do i use this to upload to S3 using Amplify?
import ImagePicker from 'react-native-image-picker';
import RNFetchBlob from 'react-native-fetch-blob';
import {Storage} from 'aws-amplify';
class App extends Component {
constructor(props) {
super(props)
}
//This function is called on a Button click
pickVideo = async () => {
const options = {
mediaType: 'video'
};
ImagePicker.launchImageLibrary(options, (response) => {
if(response.didCancel){
console.log('User cancelled image picker');
}
else if (response.error){
console.log('ImagePicker error: ', response.error);
}
else{
this.setState({
vidFileName: response.fileName,
});
console.log(response.uri);
this.putFileInS3(response.path, repsonse.filename);
}
});
}
readFile = (somefilePath) => {
return RNFetchBlob.fs.readFile(somefilePath, 'base64').then(data => new
Buffer(data, 'base64'));
}
putFileInS3 = (filePath, fileName) => {
this.readFile(filePath).then(buffer => {
Storage.put(fileName, buffer, { contentType: 'video/mp4' })
.then(() => {console.log('successfully saved to bucket');})
.catch(e => { console.log(e);});
}
}
Although i get the responses from the image picker, when trying to upload it says AWSS3Provider: Missing required key 'Bucket' in params, even though the user is logged in as an IAM user, using withAuthenticator.
Upvotes: 1
Views: 5305
Reputation: 381
You need to configure your bucket. https://aws-amplify.github.io/docs/js/storage
Some examples from the above link: 1 -
Storage.configure({
AWSS3: {
bucket: '',//Your bucket ARN;
region: ''//Specify the region your bucket was created in;
}
});
2 -
import Amplify from 'aws-amplify';
Amplify.configure({
Auth: {
identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',
region: 'XX-XXXX-X', // REQUIRED - Amazon Cognito Region
userPoolId: 'XX-XXXX-X_abcd1234', //OPTIONAL - Amazon Cognito User Pool ID
userPoolWebClientId: 'XX-XXXX-X_abcd1234',
},
Storage: {
AWSS3: {
bucket: '', //REQUIRED - Amazon S3 bucket
region: 'XX-XXXX-X', //OPTIONAL - Amazon service region
}
}
});
Upvotes: 3