Question

In: Math

Using python boto3 Check if a folder is in an S3 bucket and if not in...

Using python boto3

Check if a folder is in an S3 bucket and if not in the bucket then it creates a folder in the right directory

Solutions

Expert Solution

import boto3
import os

s3_client = boto3.client('s3')

def download_dir(prefix, local, bucket, client=s3_client):
"""
params:
- prefix: pattern to match in s3
- local: local path to folder in which to place files
- bucket: s3 bucket with target contents
- client: initialized s3 client object
"""
keys = []
dirs = []
next_token = ''
base_kwargs = {
'Bucket':bucket,
'Prefix':prefix,
}
while next_token is not None:
kwargs = base_kwargs.copy()
if next_token != '':
kwargs.update({'ContinuationToken': next_token})
results = client.list_objects_v2(**kwargs)
contents = results.get('Contents')
for i in contents:
k = i.get('Key')
if k[-1] != '/':
keys.append(k)
else:
dirs.append(k)
next_token = results.get('NextContinuationToken')
for d in dirs:
dest_pathname = os.path.join(local, d)
if not os.path.exists(os.path.dirname(dest_pathname)):
os.makedirs(os.path.dirname(dest_pathname))
for k in keys:
dest_pathname = os.path.join(local, k)
if not os.path.exists(os.path.dirname(dest_pathname)):
os.makedirs(os.path.dirname(dest_pathname))
client.download_file(bucket, k, dest_pathname)


Related Solutions

Write a program in Python that walks through a folder tree and searches for files with...
Write a program in Python that walks through a folder tree and searches for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder. The user can enter an absolute path for the start folder, or if the user does not enter a folder, the current directory is used. Likewise, the user can enter extensions to copy but if the user does not enter an extension,...
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Using the income statement from Folder Factory Financials Folder Factory Financials , prepare a pro forma...
Using the income statement from Folder Factory Financials Folder Factory Financials , prepare a pro forma income statement for 2016 if the company projects sales to be $60,000 in 2016. Balance Sheet Folder Factory, Inc. December 31, 2014 and 2015 2015 2014 Assets Cash $ 1,000 $     500 Accounts receivable 5,000 4,500 Inventory 7,000 6,000 Total current assets $13,000 $11,000 Gross fixed assets $20,000 $15,000 Less: Accumulated depreciation 10,000     9,000 Net fixed assets $10,000 $ 6,000 Total assets $23,000...
Find the center of S3 and the centralizer of each element of S3
Find the center of S3 and the centralizer of each element of S3
You will have to experiment with using chmod on the folder and the file to come...
You will have to experiment with using chmod on the folder and the file to come up with the following: Find the chmod commands for both the folder and the file that you would use to give the minimum permissions for You (the owner) to be able to edit and change the file (and I don’t want to see 777! ? ) : ___________________________________________________________ ___________________________________________________________ ___________________________________________________________
language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes...
language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes (the only requirement is that they be appropriate for display in class), If I use my own file though, you should handle as many fortunes as I put in. Make each fortune its own line, •in your main function ask the user for the name of the fortunes file.•Create a function which takes the name of the fortunes file as a parameter, open that...
In the root of the project create a folder called res. In that folder create a...
In the root of the project create a folder called res. In that folder create a file called DemoFile.txt Create a class with a static main that tests the ability to resolve and print a Path: Create an instance of a FileSystem class. Create an instance of the Path interface for the DemoFile.txt file. Print the constructed Path with System.out.println() method. Create a class that does the following: Using a pre-Java 7 solution, create a class that tests streams in...
I have a python program that must check if an integer is the sum of the...
I have a python program that must check if an integer is the sum of the squares of four consecutive prime numbers. However, when I run the program, it goes into an infinite while loop, and it doesn't give me any answer. I can only use the while, if, else, True and False commands. The code I'm using is n1=2   n2=3   n3=5   n4=7   n = int(input("n: "))   if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):     print(n1,n2,n3,n4)   else :     i = 2     pr = True     next =...
how to check if there are keys with the same values in a dictionary. python 3.0...
how to check if there are keys with the same values in a dictionary. python 3.0 for example: data = {'size': 4, 'entrance':(0, 0), 'exit':(0,0)} since the entrance and exit keys have the same value, i want the function to return None.
JAVA There is a folder named Recursive folder at the same location as these below files,...
JAVA There is a folder named Recursive folder at the same location as these below files, which contains files and folders. I have read the data of the Recursive folder and stored in a variable. Now they are required to be encrypted and decrypted. You may copy past the files to three different files and see the output. I am confused on how to write code for encrypt() and decrypt() The names of files and folders are stored in one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT