-
Notifications
You must be signed in to change notification settings - Fork 3
/
s3_to_s3_job.py
60 lines (48 loc) · 1.54 KB
/
s3_to_s3_job.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
57
58
59
60
import sys
import boto3
from awsglue import utils
class S3ToS3Job:
"""
Copies a file from a given S3 bucket into another one.
Job parameters:
source-bucket-name: The source bucket name.
source-file-path: The source file path.
target-bucket-name: The target bucket name.
target-file-path: The target file path.
"""
def __init__(self):
args = utils.getResolvedOptions(
sys.argv,
[
"source-bucket-name",
"source-file-path",
"target-bucket-name",
"target-file-path",
],
)
self._source_bucket_name = args["source_bucket_name"]
self._source_file_path = args["source_file_path"]
self._target_bucket_name = args["target_bucket_name"]
self._target_file_path = args["target_file_path"]
def run(self) -> None:
self._copy_file(
self._source_bucket_name,
self._source_file_path,
self._target_bucket_name,
self._target_file_path,
)
@classmethod
def _copy_file(
cls,
source_bucket_name: str,
source_file_path: str,
target_bucket_name: str,
target_file_path: str,
) -> None:
session = boto3.Session()
s3 = session.resource("s3")
source = {"Bucket": source_bucket_name, "Key": source_file_path}
target_bucket = s3.Bucket(target_bucket_name)
target_bucket.copy(source, target_file_path)
if __name__ == "__main__":
S3ToS3Job().run()