|
| 1 | +""" |
| 2 | +
|
| 3 | +Objects Storage |
| 4 | +=============== |
| 5 | +
|
| 6 | +Using an S3 compatible API, customers may store data in our Object Storage |
| 7 | +service and serve it without the need to manage their own storage system. |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +import boto3 |
| 12 | +import requests |
| 13 | +import secrets |
| 14 | + |
| 15 | +from urllib.parse import urlparse |
| 16 | + |
| 17 | + |
| 18 | +def test_bucket_urls(objects_endpoint, access_key, secret_key): |
| 19 | + """ We can create a bucket using the official AWS SDK for Python, upload |
| 20 | + an object, and make it available publicly. It is then available as follows: |
| 21 | +
|
| 22 | + - <bucket>.objects.rma|lpg.cloudscale.ch/<key> |
| 23 | + - objects.rma|lpg.cloudscale.ch/<bucket>/<key> |
| 24 | +
|
| 25 | + """ |
| 26 | + |
| 27 | + # Establish a connection using the official AWS SDK for Python |
| 28 | + s3 = boto3.client( |
| 29 | + 's3', |
| 30 | + endpoint_url=objects_endpoint, |
| 31 | + aws_access_key_id=access_key, |
| 32 | + aws_secret_access_key=secret_key, |
| 33 | + ) |
| 34 | + |
| 35 | + # Generate a bucket name that has not been used yet (bucket names are |
| 36 | + # global across all our object storage endpoints). |
| 37 | + bucket = f"at-{secrets.token_hex(8)}" |
| 38 | + |
| 39 | + # Create the bucket |
| 40 | + s3.create_bucket(Bucket=bucket) |
| 41 | + |
| 42 | + # Upload an object |
| 43 | + s3.put_object( |
| 44 | + Bucket=bucket, |
| 45 | + Key='key.txt', |
| 46 | + Body=b'test', |
| 47 | + ACL='public-read', |
| 48 | + ) |
| 49 | + |
| 50 | + parts = urlparse(objects_endpoint) |
| 51 | + |
| 52 | + # Read the expected URLs using an anonymous HTTP client |
| 53 | + urls = ( |
| 54 | + f"{objects_endpoint}/{bucket}/key.txt", |
| 55 | + f"{parts.scheme}://{bucket}.{parts.netloc}/key.txt", |
| 56 | + ) |
| 57 | + |
| 58 | + for url in urls: |
| 59 | + response = requests.get(url) |
| 60 | + assert response.status_code == 200 |
| 61 | + assert response.text == "test" |
0 commit comments