It was a hassle from the console when I wanted to get all the files from a particular bucket, so I made a tool that can get files at once by specifying a bucket. (Maybe it's already there ...) I tried to add not only acquisition but also deletion processing. (If something happens using this, please do so at your own risk.)
The source itself is available at here. When you start the tool, it will be as follows
This is created using tkinter.
Since the bucket name is set in the property, it can be set appropriately.
You can select whether to get or delete the file.
For the folder specification, select where to store the file when retrieving it.
The access key and secret access key are set in the property file, but if you have set the account with aws configure, this setting is unnecessary.
The part related to S3 of the source is as follows.
if aws_access_key_id =='':
s3 = boto3.resource('s3')
s3client=boto3.client('s3')
else:
s3 = boto3.resource('s3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key_id)
s3client=boto3.client('s3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key_id)
bucketName=self.inputFileName.get()
resultFolder=self.outputFolder.get()
dataBaseDir=os.path.join(resultFolder,bucketName)
executeType=EXECUTE_LIST.index(self.combo.get())
s3bucket=s3.Bucket(bucketName)
objs = s3bucket.meta.client.list_objects_v2(Bucket=s3bucket.name)
for o in objs.get('Contents'):
key = o.get('Key')
s3Paths=os.path.splitext(key)
if len(s3Paths[1]) !=0:
keys=key.split('/')
filename=keys[len(keys)-1]
if executeType==0:
outputDataDir=key.split(filename)[0]
outputDataDir=os.path.join(dataBaseDir,outputDataDir)
os.makedirs(outputDataDir,exist_ok=True)
outputDataFile=os.path.join(outputDataDir,filename)
s3bucket.download_file(key,outputDataFile)
else:
s3client.delete_object(Bucket=s3bucket.name, Key=key)
objs = s3bucket.meta.client.list_objects_v2(Bucket=s3bucket.name)
Since it can be obtained in the part of, it is processed by the for statement.
It is also possible to specify only a specific folder or file by adding Prefix
to the argument.
pythonGui.py
, it will be as follows.
Recommended Posts