在 FaaS 中使用 Node + aws-sdk,我试图获取一个预签名的 url 来将文件上传到 S3 存储桶。当我上传文件时,我得到以下响应
InvalidAccessKeyId
您提供的 AWS 访问密钥 ID 不存在于我们的记录中。
// getUploadUrl
import { FunctionEvent } from 'graphcool-lib'
import * as AWS from 'aws-sdk'
const BUCKET = process.env['BUCKET'];
const AWS_ACCESS_KEY_ID = process.env['AWS_ACCESS_KEY_ID'];
const AWS_SECRET_ACCESS_KEY = process.env['AWS_SECRET_ACCESS_KEY'];
const signedUrlExpiresSeconds = 60*10;
AWS.config.update({
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
region: 'eu-west-2'
});
const s3 = new AWS.S3({ apiVersion: "2006-03-01" })
interface EventData {
fileName: string
}
export default async (event: FunctionEvent<EventData>) => {
if (!event.context.auth || !event.context.auth.nodeId) {
return { error: 'No user logged in.' }
}
try {
const { fileName } = event.data;
// Pre-signing a putObject (asynchronously)
const params = { Bucket: BUCKET, Key: fileName, Expires: signedUrlExpiresSeconds }
const uploadUrl: string = await s3.getSignedUrl('putObject', params)
if (!uploadUrl) {
return { error: 'Unable to get presigned upload URL from S3' }
}
return { data: { uploadUrl } }
} catch (e) {
console.log(e)
return { error: 'An unexpected error occured during password change.' }
}
}
这是我对 AWS IAM 的权限
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject"
],
"Resource": "arn:aws:s3:::bucket-name/*"
}
]
}
我正在使用以下内容上传文件
curl -v -T test.pdf presignedUrl
有任何想法吗?
这是生成的路径
"https://my-aws-s3-domain.s3.eu-west-2.amazonaws.com/test.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXX1222%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20171222T153846Z&X-Amz-Expires=600&X-Amz-Signature=XXXXXX-268d&X-Amz-SignedHeaders=host"
我将 AWS 配置更新为,AWS.config.update({ region: 'eu-west-2' });
因为它会自动从 process.ENV 中获取 AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY(正如 Michael 所指出的那样)。
我又试了一次,得到了Access denied
回复,所以我将 IAM 政策更改为:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::my-s3-bucket-name"
}
]
}
它仍然给出拒绝访问错误。