Exemplo n.º 1
0
def test_write_to_s3():
    bucket = "test-bucket"
    prefix = "test-prefix/"
    dest_filename = "test.json"

    conn = boto3.resource("s3", region_name="us-west-2")
    bucket_obj = conn.create_bucket(Bucket=bucket)

    with NamedTemporaryFile() as json_file:
        json_file.write(json.dumps(SAMPLE_DATA))
        # Seek to the beginning of the file to allow the tested
        # function to find the file content.
        json_file.seek(0)
        # Upload the temp file to S3.
        taar_utils.write_to_s3(json_file.name, dest_filename, prefix, bucket)

    available_objects = list(bucket_obj.objects.filter(Prefix=prefix))
    assert len(available_objects) == 1

    # Check that our file is there.
    full_s3_name = "{}{}".format(prefix, dest_filename)
    keys = [o.key for o in available_objects]
    assert full_s3_name in keys

    stored_data = taar_utils.read_from_s3(dest_filename, prefix, bucket)
    assert SAMPLE_DATA == stored_data
Exemplo n.º 2
0
def test_read_from_s3():
    # Write a JSON blob
    bucket = "test-bucket"
    prefix = "test-prefix/"
    s3_json_fname = "test.json"

    conn = boto3.resource("s3", region_name="us-west-2")
    conn.create_bucket(Bucket=bucket)

    with NamedTemporaryFile() as json_file:
        json_file.write(json.dumps(SAMPLE_DATA))
        # Seek to the beginning of the file to allow the tested
        # function to find the file content.
        json_file.seek(0)
        # Upload the temp file to S3.
        taar_utils.write_to_s3(json_file.name, s3_json_fname, prefix, bucket)

    data = taar_utils.read_from_s3(s3_json_fname, prefix, bucket)
    assert data == SAMPLE_DATA