英文:
Files are empty after copying it to the S3 bucket
问题
我有一个名为 source-bucket-6
的S3存储桶,并编写了一个Lambda函数,用于将文件从Lambda位置复制到S3存储桶。
如您在截图中所见,这里有两个文件 Index.html
和 Test.txt
。我只想将这些文件复制到S3存储桶。但每当我尝试将它们复制到存储桶时,我看到两个文件都是空的,没有内容在文件中。这是我的Lambda代码:
import boto3
import os
s3 = boto3.client('s3')
cwd = os.getcwd()
def lambda_handler(event, context):
bucket = 'source-bucket-6'
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if f != "lambda_function.py":
s3.put_object(Bucket=bucket, Key=f)
print(f"------------------{f} ")
print("Put Completed")
英文:
I have an S3 bucket called source-bucket-6
and I write a lambda function to copy files from the lambda location to the S3 bucket.
As you see in the screenshot, here we have two files Index.html
and Test.txt
. I just want to copy those files to the S3 bucket. But whenever I try to copy them to the bucket, I see both files are empty and there's no content in the files. Here's my lambda code:
import boto3
import os
s3 = boto3.client('s3')
cwd = os.getcwd()
def lambda_handler(event, context):
bucket = 'source-bucket-6'
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if f != "lambda_function.py":
s3.put_object(Bucket=bucket, Key=f)
print(f"------------------{f} ")
print("Put Completed")
答案1
得分: 2
你的 put_object
命令没有为对象指定任何内容。
如果你想要上传一个文件,请使用:
s3.upload_file(Filename=f, Bucket=bucket, Key=f)
其中 f
是要上传的文件的文件名。
英文:
Your put_object
command is not specifying any content for the object.
If you want to upload a file, use:
s3.upload_file(Filename=f, Bucket=bucket, Key=f)
where f
is the filename of the file to upload.
答案2
得分: 0
你缺少Body参数。应该是:
s3.put_object(Bucket=bucket, Key=f, Body=open(f, 'r'))
英文:
You are missing Body argument. It should be:
s3.put_object(Bucket=bucket, Key=f, Body=open(f, 'r'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论