从其 url 下载图像并直接上传到 AWS
Download an image from its url and directly upload it to AWS
我的问题找不到满意的答案。给定一张图片 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编码
仍然损坏。我感觉非常接近解决方案,因为图像具有正确的类型和文件大小,但仍然无法在浏览器或我的计算机上打印。关于如何正确 encode/retrieve 数据的任何想法?
好的我自己找到了答案:
aldeed:meteor
允许向 get
请求添加 responseType
参数。我们只需要将此选项设置为 buffer
,以便我们将数据作为缓冲区获取。然后我们简单的给这个buffer,不做任何转换,作为上传函数的Body
。
我的问题找不到满意的答案。给定一张图片 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编码
仍然损坏。我感觉非常接近解决方案,因为图像具有正确的类型和文件大小,但仍然无法在浏览器或我的计算机上打印。关于如何正确 encode/retrieve 数据的任何想法?
好的我自己找到了答案:
aldeed:meteor
允许向 get
请求添加 responseType
参数。我们只需要将此选项设置为 buffer
,以便我们将数据作为缓冲区获取。然后我们简单的给这个buffer,不做任何转换,作为上传函数的Body
。