forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request awsdocs#1658 from Laren-AWS/Laren-AWS/s3-presignedurl
S3 presigned URL example with Requests package.
- Loading branch information
Showing
6 changed files
with
128 additions
and
74 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
""" | ||
Purpose | ||
Shows how to use the AWS SDK for Python (Boto3) with Amazon Simple Storage Service | ||
(Amazon S3) to generate a presigned URL that can perform an action for a limited | ||
time with your credentials. Also shows how to use the Requests package | ||
to make a request with the URL. | ||
""" | ||
|
||
import argparse | ||
import logging | ||
import boto3 | ||
from botocore.exceptions import ClientError | ||
import requests | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def generate_presigned_url(s3_client, client_method, method_parameters, expires_in): | ||
""" | ||
Generate a presigned Amazon S3 URL that can be used to perform an action. | ||
:param s3_client: A Boto3 Amazon S3 client. | ||
:param client_method: The name of the client method that the URL performs. | ||
:param method_parameters: The parameters of the specified client method. | ||
:param expires_in: The number of seconds the presigned URL is valid for. | ||
:return: The presigned URL. | ||
""" | ||
try: | ||
url = s3_client.generate_presigned_url( | ||
ClientMethod=client_method, | ||
Params=method_parameters, | ||
ExpiresIn=expires_in | ||
) | ||
logger.info("Got presigned URL: %s", url) | ||
except ClientError: | ||
logger.exception( | ||
"Couldn't get a presigned URL for client method '%s'.", client_method) | ||
raise | ||
return url | ||
|
||
|
||
def usage_demo(): | ||
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') | ||
|
||
print('-'*88) | ||
print("Welcome to the Amazon S3 presigned URL demo.") | ||
print('-'*88) | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('bucket', help="The name of the bucket.") | ||
parser.add_argument('key', help="The key of the object.") | ||
parser.add_argument( | ||
'action', choices=('get', 'put'), help="The action to perform.") | ||
args = parser.parse_args() | ||
|
||
s3_client = boto3.client('s3') | ||
client_action = 'get_object' if args.action == 'get' else 'put_object' | ||
url = generate_presigned_url( | ||
s3_client, client_action, {'Bucket': args.bucket, 'Key': args.key}, 1000) | ||
|
||
print("Using the Requests package to send a request to the URL.") | ||
response = None | ||
if args.action == 'get': | ||
response = requests.get(url) | ||
elif args.action == 'put': | ||
print("Putting data to the URL.") | ||
with open(args.key, 'r') as object_file: | ||
object_text = object_file.read() | ||
response = requests.put(url, data=object_text) | ||
|
||
print("Got response:") | ||
print(f"Status: {response.status_code}") | ||
print(response.text) | ||
|
||
print('-'*88) | ||
|
||
|
||
if __name__ == '__main__': | ||
usage_demo() |
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
22 changes: 22 additions & 0 deletions
22
python/example_code/s3/s3_basics/test/test_presigned_url.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
""" | ||
Unit tests for presigned_url.py functions. | ||
""" | ||
|
||
import boto3 | ||
|
||
import presigned_url | ||
|
||
|
||
def test_generate_presigned_url(): | ||
s3_client = boto3.client('s3') | ||
client_method = 'get_object' | ||
method_params = {'Bucket': 'test-bucket', 'Key': 'test-key'} | ||
expires = 10 | ||
|
||
got_url = presigned_url.generate_presigned_url( | ||
s3_client, client_method, method_params, expires) | ||
assert 'test-bucket' in got_url | ||
assert 'test-key' in got_url |