问题
I am using Expo Image Picker to send a cropped image to s3. The file is being uploaded, but it does not render as an image as its not recognised as one. If I took the blob data and use it in a base64 to image encoder I get the image, so it must be mime or encoding based, here is what I have.
I invoke the Expo Image Picker with;
let pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [1, 1],
base64: true,
exif: false,
});
The params I use to create the signed URL from s3 SDK are;
const params = {
Bucket: 'images,
Key: 'filename.jpg',
Expires: 60 * 5,
ACL: 'public-read',
ContentEncoding: 'base64',
ContentType: 'image/jpeg'
};
The upload is done with axios with the following headers;
const config = {
headers: {
'x-amz-acl' : 'public-read',
'Content-Encoding': 'base64',
'Content-Type': 'image/jpeg'
}
};
return await axios.put(`${signedURL}`,
base64data,
config)
.then(response => {
console.log('IMAGE UPLOAD SUCCESS');
return response.data;
})
If I change the created .jpg file as .txt and view it in sublime the data is the base64 encoded string rather than the usual blob data of a jpeg.
What am I doing wrong?
回答1:
Ok; found the solution;
use the buffer npm module;
import { Buffer } from 'buffer';
Added this to my function
return await axios.put(`${signedURL}`,
new Buffer(base64data, 'base64'), // make this a buffer
config)
.then(response => {
console.log('IMAGE UPLOAD SUCCESS');
return response.data;
})
来源:https://stackoverflow.com/questions/49624346/image-upload-to-s3-does-not-render