Amazon s3 使用python在S3上上载包含子文件夹和文件的文件夹
我有一个文件夹,其中包含一堆子文件夹和文件,我从服务器获取这些子文件夹和文件并将其分配给一个变量。文件夹结构如下:Amazon s3 使用python在S3上上载包含子文件夹和文件的文件夹,amazon-s3,upload,python-3.6,boto3,Amazon S3,Upload,Python 3.6,Boto3,我有一个文件夹,其中包含一堆子文件夹和文件,我从服务器获取这些子文件夹和文件并将其分配给一个变量。文件夹结构如下: └── main_folder ├── folder │ ├── folder │ │ ├── folder │ │ │ └── a.json │ │ ├── folder │ │ │ ├── folder │ │ │ │ └── b.json │ │
└── 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将这个主文件夹上传到结构相同的S3 bucket。在boto3中,无法在s3上上载文件夹
我已经在这个链接上看到了解决方案,但是他们从本地机器获取文件,我从服务器获取数据并分配到变量
有人遇到过同样类型的问题吗?下面是适合我的代码,纯Python 3
""" 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)
您是特别想自己编写代码,还是愿意使用?我只想通过代码@JohnRotensteinIt来实现,似乎你有“服务器”上的数据,你想把它放在Amazon S3的存储桶中。您可以在“服务器”上运行代码将其发送到S3,也可以在另一台计算机上运行代码从服务器检索代码,然后将其上载到S3。那么,你的问题到底是什么?你能告诉我们你面临什么问题吗?你想要类似的吗?