简体   繁体   English

如何使用 boto3 将文件从 S3 下载到自定义文件夹或网络路径

[英]How to download files from S3 to a custom folder or a network path using boto3

Below is the function to download the files from a S3 Bucket.下面是 function 从 S3 存储桶下载文件。 But the problem is I can't find how to direct those files into a network path instead of downloading into the project folder without having any control over where the files must be downloaded.但问题是我找不到如何将这些文件定向到网络路径而不是下载到项目文件夹中,而无法控制必须下载文件的位置。

import boto3
import config
import os
import win32api

def download_all_objects_in_folder():
 #= boto3.resource('s3')
s3_resource = boto3.resource('s3', aws_access_key_id=config.AWS_BUCKET_KEY, aws_secret_access_key=config.AWS_BUCKET_SECRET_KEY) 
my_bucket = s3_resource.Bucket(config.BUCKET)
# Create the folder logic here
objects = my_bucket.objects.filter(Prefix='Export_20181104/')
for obj in objects:
    path, filename = os.path.split(obj.key)
    my_bucket.download_file(obj.key, filename,"C:\Other")
    #win32api.MessageBox(0, obj.key, 'title')

print("imports completed")

Update: This is the error I am getting when I pass the custom path.更新:这是我通过自定义路径时遇到的错误。

ValueError: Invalid extra_args key 'C', must be one of: ChecksumMode, 
VersionId, SSECustomerAlgorithm, SSECustomerKey, SSECustomerKeyMD5, 
RequestPayer, ExpectedBucketOwner

There is no difference for Python or the Boto3 SDK where you download your specified S3 object, whether that is a local or network location. Python 或 Boto3 SDK 没有区别,您在哪里下载指定的 S3 object,无论是本地还是网络位置。

That concern is up to the operating system.这个问题取决于操作系统。

Download to the network location, just as you would download to the local location.下载到网络位置,就像下载到本地位置一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM