小编giu*_*osa的帖子

为什么 boto3.client.download_file 在文件名末尾附加一个字符串?

我需要从 s3 下载文件,并创建以下代码:

#This function make the download of the files in a bucket 
def download_dir(s3:boto3.client, bucket:str, directory:str=None) -> None:
    
    #Verify if exist the bucket diretory
    if not os.path.exists(bucket):
                #Creating the bucket directory
                os.makedirs(bucket)
    
    # Iterating in s3 list of objects inside of the bucket
    for s3_key in s3.list_objects(Bucket=bucket)['Contents']:
        
        file_name=os.path.join(bucket, s3_key['Key'])
        #If the object is not a directory
        if not s3_key['Key'].endswith("/"):
            #Verify if the download file was already done  
            if not os.path.exists(file_name):
                print(s3_key['Key'])
                real_file_name = s3_key['Key']
                print(real_file_name)
                try:
                    s3.download_file(Bucket=bucket,Key=s3_key['Key'], Filename=file_name) …
Run Code Online (Sandbox Code Playgroud)

python amazon-s3 amazon-web-services python-requests boto3

3
推荐指数
1
解决办法
5095
查看次数