【问题标题】:How to upload file from URL to S3 bucket (aws-sdk-js v3)?如何将文件从 URL 上传到 S3 存储桶(aws-sdk-js v3)?
【发布时间】:2021-07-17 14:00:51
【问题描述】:

我尝试了下面的代码,但没有成功,它显示了这个错误消息:

(node:88634) UnhandledPromiseRejectionWarning: NotImplemented: A header you provided implies functionality that is not implemented
import { fromIni } from '@aws-sdk/credential-provider-ini'
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
import https from 'https'
import { Readable } from 'stream'
import awsConfig from './aws-exports'

const s3Client = new S3Client({
  credentials: fromIni({ profile: 'default' }),
  region: 'ap-northeast-1',
})

async function getFileFromUrl(url: string): Promise<Readable> {
  return new Promise((resolve) => {
    https.get(url, (response) => {
      resolve(response)
    })
  })
}

async function upload(file: Readable) {
  const uploadCommand = new PutObjectCommand({
    Bucket: awsConfig.aws_user_files_s3_bucket,
    Key: 'test.jpg',
    Body: file,
    ACL: 'public-read',
  })

  await s3Client.send(uploadCommand)
}

async function migrate() {
  const file = await getFileFromUrl(
    'https://example.com/logo.png'
  )
  await upload(file)
  console.log('done')
}

migrate()

如果我将Body 更改为字符串,我可以确认没问题... 有谁知道如何正确地做到这一点? 谢谢!

【问题讨论】:

    标签: amazon-s3 aws-sdk-js


    【解决方案1】:

    这里的问题是您的 getFileFromUrl 函数不起作用,AWS 不知道如何处理您正在处理的对象。您需要等待来自 https 的数据事件,如下所示:

    async function getFileFromUrl (url) {
      return new Promise((resolve) => {
        https.get(url, (response) => {
          response.on('data', (d) => {
            resolve(d)
          })
        })
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 2018-02-07
      • 2018-04-25
      • 2020-03-17
      • 2022-01-11
      • 2012-05-28
      • 2017-09-24
      • 2021-02-07
      • 2021-09-27
      相关资源
      最近更新 更多