gpt4 book ai didi

amazon-s3 - 使用python在S3上上传包含子文件夹和文件的文件夹

转载 作者:行者123 更新时间:2023-12-02 19:58:28 28 4
gpt4 key购买 nike

我有一个包含一堆子文件夹和文件的文件夹,我正在从服务器获取这些子文件夹和文件并将其分配给一个变量。文件夹结构如下:


└── main_folder
├── folder
│   ├── folder
│   │   ├── folder
│   │   │   └── a.json
│   │   ├── folder
│   │   │   ├── folder
│   │   │   │   └── b.json
│   │   │   ├── folder
│   │   │   │   └── c.json
│   │   │   └── folder
│   │   │   └── d.json
│   │   └── folder
│   │   └── e.json
│   ├── folder
│   │   └── f.json
│   └── folder
│   └── i.json

现在我想使用 boto3 将这个 main_folder 上传到具有相同结构的 S3 存储桶。在 boto3 中,无法在 s3 上上传文件夹。

我在这个链接上看到了解决方案,但他们从本地机器获取文件,我从服务器获取数据并分配给变量。

Uploading a folder full of files to a specific folder in Amazon S3

upload a directory to s3 with boto

https://gist.github.com/feelinc/d1f541af4f31d09a2ec3

有人遇到过相同类型的问题吗?

最佳答案

下面是适合我的代码,纯 python3。

""" upload one directory from the current working directory to aws """
from pathlib import Path
import os
import glob
import boto3

def upload_dir(localDir, awsInitDir, bucketName, tag, prefix='/'):
"""
from current working directory, upload a 'localDir' with all its subcontents (files and subdirectories...)
to a aws bucket
Parameters
----------
localDir : localDirectory to be uploaded, with respect to current working directory
awsInitDir : prefix 'directory' in aws
bucketName : bucket in aws
tag : tag to select files, like *png
NOTE: if you use tag it must be given like --tag '*txt', in some quotation marks... for argparse
prefix : to remove initial '/' from file names

Returns
-------
None
"""
s3 = boto3.resource('s3')
cwd = str(Path.cwd())
p = Path(os.path.join(Path.cwd(), localDir))
mydirs = list(p.glob('**'))
for mydir in mydirs:
fileNames = glob.glob(os.path.join(mydir, tag))
fileNames = [f for f in fileNames if not Path(f).is_dir()]
rows = len(fileNames)
for i, fileName in enumerate(fileNames):
fileName = str(fileName).replace(cwd, '')
if fileName.startswith(prefix): # only modify the text if it starts with the prefix
fileName = fileName.replace(prefix, "", 1) # remove one instance of prefix
print(f"fileName {fileName}")

awsPath = os.path.join(awsInitDir, str(fileName))
s3.meta.client.upload_file(fileName, bucketName, awsPath)

if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--localDir", help="which dir to upload to aws")
parser.add_argument("--bucketName", help="to which bucket to upload in aws")
parser.add_argument("--awsInitDir", help="to which 'directory' in aws")
parser.add_argument("--tag", help="some tag to select files, like *png", default='*')
args = parser.parse_args()

# cd whatever is above your dir, then run it
# (below assuming this script is in ~/git/hu-libraries/netRoutines/uploadDir2Aws.py )
# in the example below you have directory structure ~/Downloads/IO
# you copy full directory of ~/Downloads/IO to aws bucket markus1 to 'directory' 2020/IO
# NOTE: if you use tag it must be given like --tag '*txt', in some quotation marks...

# cd ~/Downloads
# python ~/git/hu-libraries/netRoutines/uploadDir2Aws.py --localDir IO --bucketName markus1 --awsInitDir 2020
upload_dir(localDir=args.localDir, bucketName=args.bucketName,
awsInitDir=args.awsInitDir, tag=args.tag)

关于amazon-s3 - 使用python在S3上上传包含子文件夹和文件的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426471/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com