-
Notifications
You must be signed in to change notification settings - Fork 0
/
icos.py
73 lines (63 loc) · 3.02 KB
/
icos.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
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Python code snippets to work with IBM Cloud Object Storage and the `ibm_boto3` library.
ibm_boto3 can be installed using the command: `pip install ibm-cos-sdk`.
Updated the last time at: 18:26 22/AUG/2019 by @vnderlev
"""
import json
import ibm_boto3
from ibm_botocore.client import Config
# Copy the ICOS credentials from the IBM Cloud Web page and save them as a json file named below.
ICOS_JSONFILE = "icos_credentials.json"
with open(ICOS_JSONFILE) as json_file:
icos_cred = json.load(json_file)
# An `ibm_boto3.client` object is stantiated with the provided credentials.
# The endpoint_url may be different depending on the region ICOS is instantiated.
icos = ibm_boto3.client(service_name="s3",
ibm_api_key_id=icos_cred['apikey'],
ibm_service_instance_id=icos_cred['resource_instance_id'],
ibm_auth_endpoint="https://iam.cloud.ibm.com/identity/token",
config=Config(signature_version="oauth"),
endpoint_url="https://s3.us-south.cloud-object-storage.appdomain.cloud" # You can choose different endpoints.
)
# list all the available buckets at this ICOS instance using `list_buckets()`.
for bucket in icos.list_buckets()['Buckets']:
print(bucket['Name'])
# To create a bucket in ICOS use:
icos.create_bucket(Bucket='bucket_name')
# To delete a bucket in ICOS use:
icos.delete_bucket(Bucket='bucket_name')
def download_file_from_icos(icos_obj, bucket: str, local_file_name: str, key: str) -> None:
"""
Function to download a file from a specific bucket at an ICOS instance.
ARGS:
@icos_obj -> an `ibm_boto3.client` object;
@bucket -> a str with the bucket name;
@local_file_name -> a str with the directory/name where the file will be saved; and
@key -> a str with the name of the desired file inside the bucket.
"""
try:
icos_obj.download_file(Bucket=bucket, Key=key, Filename=local_file_name)
except Exception as e:
print(Exception, e)
else:
print('File `{}` downloaded from ICOS and saved locally as `{}`.'.format(key, local_file_name))
def upload_file_to_icos(icos_obj, bucket: str, local_file_name: str, key: str) -> None:
"""
Function to download a file from a specific bucket at an ICOS instance.
ARGS:
@icos_obj -> an `ibm_boto3.client` object;
@bucket -> a str with the bucket name;
@local_file_name -> a str with the directory/name where the file to be uploaded is located; and
@key -> a str with the name of the file that will be saved inside the bucket.
"""
try:
icos_obj.upload_file(Filename=local_file_name, Bucket=bucket, Key=key)
except Exception as e:
print(Exception, e)
else:
print('File `{}` uploaded to ICOS as `{}`.'.format(local_file_name, key))
# Define the desired bucket in the variable below.
BUCKET = ""
# Example usage:
download_file_from_icos(icos, BUCKET, "img2.zip", "img1.zip")
download_file_from_icos(icos, BUCKET, "img2.zip", "img1.zip")