【发布时间】:2019-11-12 08:20:06
【问题描述】:
我一直在寻找一种从预签名 URL 写入 S3 存储桶中的 JSON 文件的方法。根据我的研究,它似乎可以完成,但这些不在 Node 中:
- http PUT a file to S3 presigned URLs using ruby
- PUT file to S3 with presigned URL
- Uploading a file to a S3 Presigned URL
- Write to a AWS S3 pre-signed url using Ruby
- How to create and read .txt file with fs.writeFile to AWS Lambda
没有从我的搜索中找到节点解决方案并使用第 3 方 API 我正在尝试将回调写入 S3 存储桶中的 JSON。我可以毫无问题地生成预签名 URL,但是当我尝试将虚拟文本写入预签名 URL 时,我得到:
错误:ENOENT:没有这样的文件或目录,打开 'https://path-to-file-with-signed-url'
当我尝试使用writeFile:
fs.writeFile(testURL, `This is a write test: ${Date.now()}`, function(err) {
if(err) return err
console.log("File written to")
})
我对@987654331@ 下的documentation 的理解表明我可以使用URL。我开始相信这可能是权限问题,但我在文档中找不到任何运气。
在实现node-fetch 后,我仍然收到错误 (403 Forbidden) 根据预签名 URL 写入 S3 中的文件,这是我编写的模块的完整代码:
const aws = require('aws-sdk')
const config = require('../config.json')
const fetch = require('node-fetch')
const expireStamp = 604800 // 7 days
const existsModule = require('./existsModule')
module.exports = async function(toSignFile) {
let checkJSON = await existsModule(`${toSignFile}.json`)
if (checkJSON == true) {
let testURL = await s3signing(`${toSignFile}.json`)
fetch(testURL, {
method: 'PUT',
body: JSON.stringify(`This is a write test: ${Date.now()}`),
}).then((res) => {
console.log(res)
}).catch((err) => {
console.log(`Fetch issue: ${err}`)
})
}
}
async function s3signing(signFile) {
const s3 = new aws.S3()
aws.config.update({
accessKeyId: config.aws.accessKey,
secretAccessKey: config.aws.secretKey,
region: config.aws.region,
})
params = {
Bucket: config.aws.bucket,
Key: signFile,
Expires: expireStamp
}
try {
// let signedURL = await s3.getSignedUrl('getObject', params)
let signedURL = await s3.getSignedUrl('putObject', params)
console.log('\x1b[36m%s\x1b[0m', `Signed URL: ${signedURL}`)
return signedURL
} catch (err) {
return err
}
}
查看权限我在权限中设置了上传和写入权限没有问题。在 Node 中,如何使用该文件的预签名 URL 作为路径写入 S3 存储桶中的文件?
【问题讨论】:
-
您确定您用于签署请求的 AWS 凭证实际上允许 s3:PutObject 到该 S3 存储桶吗?也许用 awscli 验证这一点。另请参阅aws.amazon.com/premiumsupport/knowledge-center/… 了解有关诊断 403 的一些建议(使用控制台,但内容仍然有价值)。
标签: node.js amazon-web-services url amazon-s3 aws-lambda