示例#1
0
def test_create_bucket_no_region(stub_and_patch, make_unique_name):
    """Test that creating a bucket with no Region raises an error."""
    stubber = stub_and_patch(bucket_wrapper, 'get_s3')
    bucket_name = make_unique_name('bucket')

    stubber.stub_create_bucket(bucket_name,
                               error_code='IllegalLocationConstraintException')

    with pytest.raises(ClientError):
        bucket_wrapper.create_bucket(bucket_name)
示例#2
0
def test_create_existing_bucket(stub_and_patch, make_unique_name, make_bucket):
    """Test that creating an existing bucket raises an error."""
    stubber = stub_and_patch(bucket_wrapper, 'get_s3')
    bucket = make_bucket(stubber, bucket_wrapper.get_s3())

    stubber.stub_create_bucket(bucket.name, stubber.region_name,
                               error_code='BucketAlreadyOwnedByYou')

    with pytest.raises(stubber.client.exceptions.BucketAlreadyOwnedByYou):
        bucket_wrapper.create_bucket(bucket.name, stubber.region_name)
示例#3
0
def test_create_bucket_no_region(make_stubber, make_unique_name):
    """Test that creating a bucket with no Region raises an error."""
    stubber = make_stubber(bucket_wrapper, 'get_s3', 'us-west-2')
    bucket_name = make_unique_name('bucket')

    stubber.stub_create_bucket_error(bucket_name,
                                     'IllegalLocationConstraintException')

    with pytest.raises(ClientError):
        bucket_wrapper.create_bucket(bucket_name)
示例#4
0
def test_create_existing_bucket(make_stubber, make_unique_name, make_bucket):
    """Test that creating an existing bucket raises an error."""
    stubber = make_stubber(bucket_wrapper, 'get_s3')
    bucket_name = make_unique_name('bucket')

    make_bucket(stubber, bucket_wrapper, bucket_name, stubber.region_name)

    stubber.stub_create_bucket_error(bucket_name, 'BucketAlreadyOwnedByYou',
                                     stubber.region_name)

    with pytest.raises(stubber.client.exceptions.BucketAlreadyOwnedByYou):
        bucket_wrapper.create_bucket(bucket_name, stubber.region_name)
示例#5
0
def test_delete_empty_bucket(stub_and_patch, make_unique_name, make_bucket):
    """Test that deleting an empty bucket works as expected."""
    stubber = stub_and_patch(bucket_wrapper, 'get_s3')
    bucket_name = make_unique_name('bucket')

    stubber.stub_create_bucket(bucket_name, stubber.region_name)
    stubber.stub_head_bucket(bucket_name)
    bucket = bucket_wrapper.create_bucket(bucket_name, stubber.region_name)

    stubber.stub_delete_bucket(bucket.name)
    stubber.stub_head_bucket(bucket.name, 404)
    bucket_wrapper.delete_bucket(bucket)
示例#6
0
def usage_demo():
    """Demonstrated ways to use the functions in this module."""
    bucket = bucket_wrapper.create_bucket(
        'usage-demo-object-wrapper-' + str(uuid.uuid1()),
        bucket_wrapper.s3_resource.meta.client.meta.region_name)

    object_key = os.path.split(__file__)[-1]
    put_object(bucket, object_key, __file__)
    print(f"Put file object with key {object_key} in bucket {bucket.name}.")

    with open(__file__) as file:
        lines = file.readlines()

    for _ in range(10):
        line = random.randint(0, len(lines))
        put_object(bucket, f'line-{line}', bytes(lines[line], 'utf-8'))
    print(f"Put 10 random lines from this script as objects.")

    listed_lines = list_objects(bucket, 'line-')
    print(f"They are: {', '.join(l.key for l in listed_lines)}")

    line_to_delete = listed_lines.pop()
    line_body = get_object(bucket, line_to_delete.key)
    print(f"Got object with key {line_to_delete.key} and body {line_body}.")
    delete_object(bucket, line_to_delete.key)
    print(f"Deleted object with key {line_to_delete.key}.")

    copy_key = listed_lines[0].key + '-copy'
    copy_object(bucket, listed_lines[0].key, bucket, copy_key)
    print(f"Made a copy of object {listed_lines[0].key}, named {copy_key}.")

    try:
        put_acl(bucket, copy_key, '*****@*****.**')
        acl = get_acl(bucket, copy_key)
        print(
            f"Put ACL grants on object {object_key}: {json.dumps(acl.grants)}")
    except ClientError as error:
        if error.response['Error'][
                'Code'] == 'UnresolvableGrantByEmailAddress':
            print(
                "Couldn't apply the ACL to the object because the specified "
                "email is for a test user who does not exist. For this request to "
                "succeed, you must replace the user email with one for an "
                "actual AWS user.")
        else:
            raise

    empty_bucket(bucket)
    print(f"Emptied bucket {bucket.name} in preparation for deleting it.")

    bucket_wrapper.delete_bucket(bucket)
    print(f"Deleted bucket {bucket.name}.")
示例#7
0
def test_create_bucket(stub_and_patch, make_unique_name, region_name):
    """Test creating a bucket in various AWS Regions."""
    stubber = stub_and_patch(bucket_wrapper, 'get_s3', region_name)
    bucket_name = make_unique_name('bucket')

    stubber.stub_create_bucket(bucket_name, region_name)
    stubber.stub_head_bucket(bucket_name)

    bucket = bucket_wrapper.create_bucket(bucket_name, region_name)
    assert bucket_name == bucket.name

    if not stubber.use_stubs:
        bucket_wrapper.delete_bucket(bucket)
def usage_demo():
    print('-' * 88)
    print("Welcome to the Amazon S3 object demo!")
    print('-' * 88)

    logging.basicConfig(level=logging.INFO,
                        format='%(levelname)s: %(message)s')

    bucket = bucket_wrapper.create_bucket(
        'usage-demo-object-wrapper-' + str(uuid.uuid1()),
        bucket_wrapper.s3_resource.meta.client.meta.region_name)

    object_key = os.path.split(__file__)[-1]
    put_object(bucket, object_key, __file__)
    print(f"Put file object with key {object_key} in bucket {bucket.name}.")

    with open(__file__) as file:
        lines = file.readlines()

    for _ in range(10):
        line = random.randint(0, len(lines))
        put_object(bucket, f'line-{line}', bytes(lines[line], 'utf-8'))
    print(f"Put 10 random lines from this script as objects.")

    listed_lines = list_objects(bucket, 'line-')
    print(f"They are: {', '.join(l.key for l in listed_lines)}")

    line_to_delete = listed_lines.pop()
    line_body = get_object(bucket, line_to_delete.key)
    print(f"Got object with key {line_to_delete.key} and body {line_body}.")
    delete_object(bucket, line_to_delete.key)
    print(f"Deleted object with key {line_to_delete.key}.")

    copy_key = listed_lines[0].key + '-copy'
    copy_object(bucket, listed_lines[0].key, bucket, copy_key)
    print(f"Made a copy of object {listed_lines[0].key}, named {copy_key}.")

    try:
        put_acl(bucket, copy_key, '*****@*****.**')
        acl = get_acl(bucket, copy_key)
        print(
            f"Put ACL grants on object {object_key}: {json.dumps(acl.grants)}")
    except ClientError as error:
        if error.response['Error'][
                'Code'] == 'UnresolvableGrantByEmailAddress':
            print('*' * 88)
            print(
                "This demo couldn't apply the ACL to the object because the email\n"
                "address specified as the grantee is for a test user who does not\n"
                "exist. For this request to succeed, you must replace the grantee\n"
                "email with one for an existing AWS user.")
            print('*' * 88)
        else:
            raise

    empty_bucket(bucket)
    print(f"Emptied bucket {bucket.name} in preparation for deleting it.")

    bucket_wrapper.delete_bucket(bucket)
    print(f"Deleted bucket {bucket.name}.")
    print("Thanks for watching!")
    print('-' * 88)