Created
May 17, 2019 14:52
-
-
Save thekoma/e0f7022a42751f52dfab42bd33c33774 to your computer and use it in GitHub Desktop.
Ceph boto3 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import logging | |
import boto3 | |
import datetime | |
import botocore | |
import os | |
access_key = 'test' | |
secret_key = 'test' | |
endpoint_url = 'http://127.0.0.1:8000' | |
bucket_name = 'ihateceph' | |
bucket_name2 = 'iloveceph' | |
# Connect | |
print '\n=======================[Connect]=======================' | |
s3 = boto3.client('s3',None, | |
aws_access_key_id=access_key, | |
aws_secret_access_key=secret_key, | |
endpoint_url=endpoint_url, | |
) | |
# Create | |
print '\n=======================[Creating bucket' + bucket_name + ']=======================' | |
s3.create_bucket(Bucket=bucket_name) | |
print '\n=======================[Listing buckets]=======================' | |
response = s3.list_buckets() | |
if 'Buckets' in response.keys(): | |
for bucket in response['Buckets']: | |
print bucket['Name'] | |
else: | |
print 'Empty' | |
print '\n=======================[Deleting bucket' + bucket_name + ']=======================' | |
# Delete | |
s3.delete_bucket(Bucket=bucket_name) | |
print '\n=======================[Listing buckets]=======================' | |
response = s3.list_buckets() | |
if 'Buckets' in response.keys(): | |
for bucket in response['Buckets']: | |
print bucket['Name'] | |
else: | |
print 'Empty' | |
# Create 2 | |
print '\n=======================[Creating bucket' + bucket_name2 + ']=======================' | |
print('Creating bucket:' + bucket_name2) | |
s3.create_bucket(Bucket=bucket_name2) | |
print '\n=======================[Listing buckets]=======================' | |
response = s3.list_buckets() | |
if 'Buckets' in response.keys(): | |
for bucket in response['Buckets']: | |
print bucket['Name'] | |
else: | |
print 'Empty' | |
print '\n=======================[Create Content]=======================' | |
response = s3.put_object( | |
Key='helloworld', | |
ACL='public-read', | |
Body='aaaaaa', | |
Bucket=bucket_name2, | |
) | |
print '\n=======================[Get String Content]=======================' | |
response = s3.get_object( | |
Bucket=bucket_name2, | |
Key='helloworld', | |
Range='bytes=2-3' | |
) | |
if 'Body' in response.keys(): | |
print response['Body'].read() | |
#print response.keys() | |
print '\n=======================[List Content]=======================' | |
response = s3.list_objects( | |
Bucket=bucket_name2, | |
) | |
if 'Contents' in response.keys(): | |
for obj in response['Contents']: | |
print obj['Key'] | |
else: | |
print 'Empty' | |
print '\n=======================[Delete Content]=======================' | |
response = s3.delete_object( | |
Bucket=bucket_name2, | |
Key='helloworld', | |
) | |
print '\n=======================[List Content]=======================' | |
response = s3.list_objects( | |
Bucket=bucket_name2, | |
) | |
if 'Contents' in response.keys(): | |
for obj in response['Contents']: | |
print obj['Key'] | |
else: | |
print 'Empty' | |
print '\n=======================[Multipart Object]=======================' | |
key='oggetto_multipart' | |
mpu = s3.create_multipart_upload(Bucket=bucket_name2, Key=key) | |
file=open("bigfile") | |
part1 = s3.upload_part(Bucket=bucket_name2, Key=key, PartNumber=1, | |
#UploadId=mpu['UploadId'], Body='Hello, world! (Parte 1)\n') | |
UploadId=mpu['UploadId'], Body=file) | |
part2 = s3.upload_part(Bucket=bucket_name2, Key=key, PartNumber=2, | |
UploadId=mpu['UploadId'], Body=file) | |
#UploadId=mpu['UploadId'], Body='Hello, world! (Parte 2)\n') | |
part_info = { | |
'Parts': [ | |
{ | |
'PartNumber': 1, | |
'ETag': part1['ETag'] | |
}, | |
{ | |
'PartNumber': 2, | |
'ETag': part2['ETag'] | |
} | |
] | |
} | |
s3.complete_multipart_upload(Bucket=bucket_name2, Key=key, UploadId=mpu['UploadId'], | |
MultipartUpload=part_info) | |
print '\n=======================[List Content]=======================' | |
response = s3.list_objects( | |
Bucket=bucket_name2, | |
) | |
if 'Contents' in response.keys(): | |
for obj in response['Contents']: | |
print obj['Key'] | |
else: | |
print 'Empty' | |
print '\n=======================[Delete Content]=======================' | |
response = s3.delete_object( | |
Bucket=bucket_name2, | |
Key=key, | |
) | |
print '\n=======================[List Content]=======================' | |
response = s3.list_objects( | |
Bucket=bucket_name2, | |
) | |
if 'Contents' in response.keys(): | |
for obj in response['Contents']: | |
print obj['Key'] | |
else: | |
print 'Empty' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment