-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
s3_create_tar.py
56 lines (46 loc) · 1.76 KB
/
s3_create_tar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Author : Avinash Dalvi
#
# This script allows you to create tar file creation.
#
# Reference question : https://stackoverflow.com/questions/64341192/how-to-create-a-tar-file-containing-all-the-files-in-a-directory/64341789#64341789
import boto3
import tarfile
import os.path
s3Client = boto3.client("s3")
s3object = boto3.resource("s3")
def lambda_handler(event, context):
agtBucket = "angularbuildbucket"
key = ""
tar = tarfile.open("/tmp/example.tar", "w")
source_dir = "/tmp/"
for fname in get_matching_s3_keys(bucket=agtBucket, prefix=key, suffix=".js"):
print(fname)
# file_obj = s3object.Object(agtBucket, fname)
# file_content = file_obj.get()['Body'].read()
# tar.add(file_content)
s3object.Bucket(agtBucket).download_file(fname, "/tmp/" + fname)
tar.add(source_dir, arcname=os.path.basename(source_dir))
tar.close()
s3object.meta.client.upload_file(
source_dir + "example.tar", agtBucket, "example.tar"
)
def get_matching_s3_keys(bucket, prefix="", suffix=""):
"""
Generate the keys in an S3 bucket.
:param bucket: Name of the S3 bucket.
:param prefix: Only fetch keys that start with this prefix (optional).
:param suffix: Only fetch keys that end with this suffix (optional).
"""
kwargs = {"Bucket": bucket, "Prefix": prefix}
while True:
resp = s3Client.list_objects_v2(**kwargs)
for obj in resp["Contents"]:
key = obj["Key"]
if key.endswith(suffix):
yield key
try:
kwargs["ContinuationToken"] = resp["NextContinuationToken"]
except KeyError:
break
if __name__ == "__main__":
lambda_handler({"invokingEvent": '{"messageType":"ScheduledNotification"}'}, None)