我的问题找不到满意的答案。给定一个图像 url,我想下载它(不将其保存到磁盘)并立即将其上传到 AWS Bucket。这是我的代码:
self.downloadImage = function(url){
let response = HTTP.get(url, {
encoding:null // for binary
})
if (!response.headers['content-type'].split('/')[0] == 'image'){
throw new Error("not an image")
}
return {
data : response.content,
contentType : response.headers['content-type']
}
}
self.uploadImage = function(websiteName, imgUrl, callback){
// we retrieve the image
let image = downloadImage(imgUrl)
let imageName = self.extractImageName(imgUrl)
let filename = 'img/' +websiteName + "/" + imageName
let newUrl = `https://s3.${Meteor.settings.AWS.REGION}.amazonaws.com/${Meteor.settings.S3.BUCKET_NAME}/${filename}`
// makes the async function sync like
let putObjectSync = Meteor.wrapAsync(s3Client.putObject, s3Client)
try{
let res = putObjectSync({
Body: image.data,
ContentType : image.contentType,
ContentEncoding: 'base64',
Key: filename,
ACL:'public-read',
Bucket: Meteor.settings.S3.BUCKET_NAME
})
return newUrl
} catch(e) {
return null
}
}
一切正常,除了图像似乎已损坏。到目前为止我试过了:
使用
aldeed:http
,以便在下载时将编码设置为null
,这对于图片来说似乎是一个不错的策略不使用,直接将响应的文本内容作为上传正文传递
在aws中添加base64编码
仍然损坏。我感觉非常接近解决方案,因为图像具有正确的类型和文件大小,但仍然无法在浏览器或我的计算机上打印。关于如何正确编码/检索数据的任何想法?
最佳答案
好吧,我自己找到了答案:
aldeed:meteor
允许向 get
请求添加一个 responseType
参数。我们只需要将此选项设置为 buffer
,这样我们就可以将数据作为缓冲区获取。然后我们简单地将这个缓冲区作为上传函数的 Body
,不进行任何转换。
关于http - 从其 url 下载图像并直接上传到 AWS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104628/