我正在使用Serverless框架和无服务器-S3-本地插件在开发期间测试我的代码。然而,尽管处于脱机模式,真正的S3桶仍被写入其中。当处于脱机模式时,我如何更改配置以使用本地假s3桶?
相关serverless.yml部分:
plugins:
- serverless-stack-output
- serverless-plugin-include-dependencies
- serverless-layers
- serverless-deployment-bucket
- serverless-s3-local
- serverless-offline
custom:
#...
s3:
bucketName: test-s3-buck
host: localhost
serverless-offline:
ignoreJWTSignature: true
httpPort: 4000
noAuth: true
directory: /tmp
resources:
Resources:
#...
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.s3.bucketName}端点调用S3:
import boto3
def post(event, context):
s3_path = "/test.txt"
body = "test"
encoded_string = body.encode("utf-8")
s3 = boto3.resource("s3")
bucket_name = "test-s3-buck"
s3.Bucket(bucket_name).put_object(Key=s3_path, Body=encoded_string)
response = {
"statusCode": 200,
"body": "Created."
}
return response离线启动服务器的:
serverless offline start发布于 2021-03-26 04:30:02
在无服务器-S3-本地中的自述文件中,我们有:
const S3 = new AWS.S3({
s3ForcePathStyle: true,
accessKeyId: 'S3RVER', // This specific key is required when working offline
secretAccessKey: 'S3RVER',
endpoint: new AWS.Endpoint('http://localhost:4569'),
});您可以实现同用 boto
import boto3
client = boto3.client(
's3',
aws_access_key_id='S3RVER',
aws_secret_access_key='S3RVER'
)这意味着,在运行serverless offline start时,需要将aws访问密钥id设置为S3RVER,将aws秘密访问密钥设置为S3RVER,否则将使用真正的桶。
在自述文件中,还有关于设置s3local aws配置文件https://github.com/ar90n/serverless-s3-local#triggering-aws-events-offline的说明
实现这一目标的另一种方法是使用环境变量运行命令:
AWS_ACCESS_KEY_ID=S3RVER AWS_SECRET_ACCESS_KEY=S3RVER serverless offline start这样,代码中的aws将读取脱机模式的正确值。
https://stackoverflow.com/questions/66810222
复制相似问题