Last active
August 16, 2023 16:24
-
-
Save ajorg-aws/13e3b7d224d18a3e0c68b1174861ac09 to your computer and use it in GitHub Desktop.
Quick hack to upload stdin to S3.
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 | |
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
# software and associated documentation files (the "Software"), to deal in the Software | |
# without restriction, including without limitation the rights to use, copy, modify, | |
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# SPDX-License-Identifier: MIT-0 | |
"""Quick hack to upload stdin to S3.""" | |
import sys | |
try: | |
from urllib.parse import urlparse | |
except ImportError: | |
from urlparse import urlparse | |
import boto3 | |
def s3urlparse(url): | |
"""Returns bucket, key from s3://bucket/key.""" | |
parsed = urlparse(url) | |
if parsed.scheme != 's3': | |
raise ValueError('Unknown scheme: %s' % parsed.scheme) | |
return parsed.hostname, parsed.path.lstrip('/') | |
def main(): | |
"""Uploads stdin to S3.""" | |
bucket, key = s3urlparse(sys.argv[1]) | |
client = boto3.client('s3') | |
try: | |
stdin = sys.stdin.buffer | |
except AttributeError: | |
stdin = sys.stdin | |
client.upload_fileobj(stdin, Bucket=bucket, Key=key) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment