Created
November 11, 2020 17:44
-
-
Save clayg/b59b5c98c66c37dffafd9169b2121bd3 to your computer and use it in GitHub Desktop.
s3 client for playing with slow ranged reads
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
from argparse import ArgumentParser | |
import sys | |
import time | |
import boto3 | |
parser = ArgumentParser() | |
parser.add_argument('bucket', help='bucket to make the upload') | |
parser.add_argument('key', nargs='?', help='key name for large object') | |
parser.add_argument('--chunk-size', '-c', type=int, default=10000, | |
help='how big to read') | |
parser.add_argument('--slow-chunks', '-S', type=int, default=0, | |
help='number of chunks to sleep before high gear') | |
parser.add_argument('--sleep', '-s', type=float, default=0.01, | |
help='how long to rest') | |
parser.add_argument('--abort', '-A', type=float, default=None, | |
help='if set quit after this long') | |
parser.add_argument('--range', '-R', default='', | |
help='throw a range on there') | |
def consume_readable(args, body): | |
start = time.time() | |
chunks_read = 0 | |
bytes_read = 0 | |
if args.abort is not None and (time.time() - start) >= args.abort: | |
return bytes_read | |
chunk = body.read(args.chunk_size) | |
while chunk: | |
chunks_read += 1 | |
bytes_read += len(chunk) | |
print('read %s bytes' % len(chunk)) | |
if chunks_read < args.slow_chunks: | |
time.sleep(args.sleep) | |
if args.abort and (time.time() - start) >= args.abort: | |
return bytes_read | |
chunk = body.read(args.chunk_size) | |
return bytes_read | |
def main(): | |
args = parser.parse_args() | |
client = boto3.client('s3', endpoint_url='http://saio:8080', | |
aws_access_key_id='test:tester', | |
aws_secret_access_key='testing') | |
resp = client.get_object(Bucket=args.bucket, Key=args.key, | |
Range=args.range) | |
bytes_read = consume_readable(args, resp['Body']) | |
print('read %s/%s bytes' % (bytes_read, resp['ContentLength'])) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment