http - 从其 url 下载图像并直接上传到 AWS

标签 http amazon-web-services meteor download upload

我的问题找不到满意的答案。给定一个图像 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/

相关文章:

sockets - 301 与 socket.http 永久移动

node.js - 在 AWS lambda 上列出 cognito 用户池用户

Meteor template.rendered - 为什么集合是空的?

javascript - 我如何模拟缓慢的 Meteor 发布?

http - 注册后重定向到使用用户凭据登录?

asp.net - 在 ASP.Net 中使用请求和响应

python-3.x - 将 Picked 或 Joblib 预训练的 ML 模型加载到 Sagemaker 并作为端点托管

amazon-web-services - 适用于 Windows PowerShell 源代码的 AWS 工具在哪里

javascript - 使用 Meteor 和 Meteor Up 时如何监控服务器端日志

android - 如何通过 expo 插件向 AndroidManifest 添加权限?