无法打开使用MongoDB Stitch上传到S3的图像
我使用MongoDB Stitch使用React Native上传图像,我可以成功上传文本和看起来像图像的内容,但我无法从S3打开图像 我在Stitch函数()中使用了他们示例中的以下代码: 从客户端,我用它来选择图像,然后像这样上传:无法打开使用MongoDB Stitch上传到S3的图像,mongodb,react-native,amazon-s3,mongodb-stitch,Mongodb,React Native,Amazon S3,Mongodb Stitch,我使用MongoDB Stitch使用React Native上传图像,我可以成功上传文本和看起来像图像的内容,但我无法从S3打开图像 我在Stitch函数()中使用了他们示例中的以下代码: 从客户端,我用它来选择图像,然后像这样上传: handleSelectedImage = response => { if (response.didCancel) { console.log('User cancelled image picker') } else if
handleSelectedImage = response => {
if (response.didCancel) {
console.log('User cancelled image picker')
} else if (response.error) {
console.log('ImagePicker Error: ', response.error)
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton)
} else {
const source = { uri: response.uri }
//const s = { uri: 'data:image/jpeg;base64,' + response.data }
const imageId = generateUniqueId()
const fileType = '.jpg'
const key = imageId + fileType
uploadImage(key, source.uri).then(result => {
console.log('Did upload image!', result)
})
}
}
以及调用Stitch函数的客户端函数:
export const uploadImage = (key, body) => {
const client = Stitch.defaultAppClient
return client.callFunction('uploadImage', [key, body])
}
我可以成功地将似乎是图像文件的内容上载到S3,但当我下载该文件时,我无法打开它:
您必须将base64图像转换为BSON。二进制:
context.services.get("MY_S3_SERVICE").s3("us-east-1").PutObject({
Bucket: "my_bucket_on_s3",
Key: "hello.jpg",
ContentType: "image/jpeg",
Body: BSON.Binary.fromBase64("iVBORw0KGgoAA... (rest of the base64 string)", 0),
})
context.services.get("MY_S3_SERVICE").s3("us-east-1").PutObject({
Bucket: "my_bucket_on_s3",
Key: "hello.jpg",
ContentType: "image/jpeg",
Body: BSON.Binary.fromBase64("iVBORw0KGgoAA... (rest of the base64 string)", 0),
})