From c482605fc842347426ec36da810097edc9d8f595 Mon Sep 17 00:00:00 2001 From: "Bala.FA" Date: Fri, 27 Nov 2020 09:39:07 +0530 Subject: [PATCH] Rename presigned_url() to get_presigned_url() --- docs/API.md | 88 ++++++++++++++++++++++++++------ examples/get_presigned_url.py | 57 +++++++++++++++++++++ examples/presigned_get_object.py | 30 ++++++----- examples/presigned_put_object.py | 34 ++++++------ minio/api.py | 81 ++++++++++------------------- 5 files changed, 191 insertions(+), 99 deletions(-) create mode 100644 examples/get_presigned_url.py diff --git a/docs/API.md b/docs/API.md index a78d87839..5dda00d23 100644 --- a/docs/API.md +++ b/docs/API.md @@ -53,7 +53,7 @@ s3Client = Minio( | [`set_bucket_policy`](#set_bucket_policy) | [`presigned_get_object`](#presigned_get_object) | | [`delete_bucket_notification`](#delete_bucket_notification) | [`presigned_put_object`](#presigned_put_object) | | [`get_bucket_notification`](#get_bucket_notification) | [`presigned_post_policy`](#presigned_post_policy) | -| [`set_bucket_notification`](#set_bucket_notification) | | +| [`set_bucket_notification`](#set_bucket_notification) | [`get_presigned_url`](#get_presigned_url) | | [`listen_bucket_notification`](#listen_bucket_notification) | | | [`delete_bucket_encryption`](#delete_bucket_encryption) | | | [`get_bucket_encryption`](#get_bucket_encryption) | | @@ -1420,15 +1420,15 @@ __Return Value__ __Example__ ```py -# Get presigned URL string to download 'my-objectname' in -# 'my-bucketname' with default expiry. -url = minio.presigned_get_object("my-bucketname", "my-objectname") +# Get presigned URL string to download 'my-object' in +# 'my-bucket' with default expiry (i.e. 7 days). +url = client.presigned_get_object("my-bucket", "my-object") print(url) -# Get presigned URL string to download 'my-objectname' in -# 'my-bucketname' with two hours expiry. -url = minio.presigned_get_object( - "my-bucketname", "my-objectname", expires=timedelta(hours=2), +# Get presigned URL string to download 'my-object' in +# 'my-bucket' with two hours expiry. +url = client.presigned_get_object( + "my-bucket", "my-object", expires=timedelta(hours=2), ) print(url) ``` @@ -1456,15 +1456,15 @@ __Return Value__ __Example__ ```py -# Get presigned URL string to upload data to 'my-objectname' in -# 'my-bucketname' with default expiry. -url = minio.presigned_put_object("my-bucketname", "my-objectname") +# Get presigned URL string to upload data to 'my-object' in +# 'my-bucket' with default expiry (i.e. 7 days). +url = client.presigned_put_object("my-bucket", "my-object") print(url) -# Get presigned URL string to upload data to 'my-objectname' in -# 'my-bucketname' with two hours expiry. -url = minio.presigned_put_object( - "my-bucketname", "my-objectname", expires=timedelta(hours=2), +# Get presigned URL string to upload data to 'my-object' in +# 'my-bucket' with two hours expiry. +url = client.presigned_put_object( + "my-bucket", "my-object", expires=timedelta(hours=2), ) print(url) ``` @@ -1500,6 +1500,64 @@ policy.add_content_length_range_condition( form_data = client.presigned_post_policy(policy) ``` +### get_presigned_url(method, bucket_name, object_name, expires=timedelta(days=7), response_headers=None, request_date=None, version_id=None, extra_query_params=None) + +Get presigned URL of an object for HTTP method, expiry time and custom request parameters. + +__Parameters__ +| Param | Type | Description | +|:---------------------|:---------------------|:---------------------------------------------------------------------------------------------------------------------| +| `method` | _str_ | HTTP method. | +| `bucket_name` | _str_ | Name of the bucket. | +| `object_name` | _str_ | Object name in the bucket. | +| `expires` | _datetime.timedelta_ | Expiry in seconds; defaults to 7 days. | +| `response_headers` | _dict_ | Optional response_headers argument to specify response fields like date, size, type of file, data about server, etc. | +| `request_date` | _datetime.datetime_ | Optional request_date argument to specify a different request date. Default is current date. | +| `version_id` | _str_ | Version ID of the object. | +| `extra_query_params` | _dict_ | Extra query parameters for advanced usage. | + +__Return Value__ + +| Return | +|:-----------| +| URL string | + +__Example__ + +```py +# Get presigned URL string to delete 'my-object' in +# 'my-bucket' with one day expiry. +url = client.get_presigned_url( + "DELETE", + "my-bucket", + "my-object", + expires=timedelta(days=1), +) +print(url) + +# Get presigned URL string to upload 'my-object' in +# 'my-bucket' with response-content-type as application/json +# and one day expiry. +url = client.get_presigned_url( + "PUT", + "my-bucket", + "my-object", + expires=timedelta(days=1), + response_headers={"response-content-type": "application/json"}, +) +print(url) + +# Get presigned URL string to download 'my-object' in +# 'my-bucket' with two hours expiry. +url = client.get_presigned_url( + "GET", + "my-bucket", + "my-object", + expires=timedelta(hours=2), +) +print(url) +``` + ## 5. Explore Further - [MinIO Golang Client SDK Quickstart Guide](https://docs.min.io/docs/golang-client-quickstart-guide) diff --git a/examples/get_presigned_url.py b/examples/get_presigned_url.py new file mode 100644 index 000000000..5532317bb --- /dev/null +++ b/examples/get_presigned_url.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# MinIO Python Library for Amazon S3 Compatible Cloud Storage, +# (C) 2020 MinIO, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from datetime import timedelta + +from minio import Minio + +client = Minio( + "play.min.io", + access_key="Q3AM3UQ867SPQQA43P2F", + secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", +) + +# Get presigned URL string to delete 'my-object' in +# 'my-bucket' with one day expiry. +url = client.get_presigned_url( + "DELETE", + "my-bucket", + "my-object", + expires=timedelta(days=1), +) +print(url) + +# Get presigned URL string to upload 'my-object' in +# 'my-bucket' with response-content-type as application/json +# and one day expiry. +url = client.get_presigned_url( + "PUT", + "my-bucket", + "my-object", + expires=timedelta(days=1), + response_headers={"response-content-type": "application/json"}, +) +print(url) + +# Get presigned URL string to download 'my-object' in +# 'my-bucket' with two hours expiry. +url = client.get_presigned_url( + "GET", + "my-bucket", + "my-object", + expires=timedelta(hours=2), +) +print(url) diff --git a/examples/presigned_get_object.py b/examples/presigned_get_object.py index fb8a13abd..fbf8a90ee 100644 --- a/examples/presigned_get_object.py +++ b/examples/presigned_get_object.py @@ -14,20 +14,24 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and my-objectname -# are dummy values, please replace them with original values. +from datetime import timedelta from minio import Minio -from minio.error import ResponseError -client = Minio('s3.amazonaws.com', - access_key='YOUR-ACCESSKEYID', - secret_key='YOUR-SECRETACCESSKEY') +client = Minio( + "play.min.io", + access_key="Q3AM3UQ867SPQQA43P2F", + secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", +) -# presigned get object URL for object name, expires in 7 days. -try: - print(client.presigned_get_object('my-bucketname', 'my-objectname')) -# Response error is still possible since internally presigned does get -# bucket location. -except ResponseError as err: - print(err) +# Get presigned URL string to download 'my-object' in +# 'my-bucket' with default expiry (i.e. 7 days). +url = client.presigned_get_object("my-bucket", "my-object") +print(url) + +# Get presigned URL string to download 'my-object' in +# 'my-bucket' with two hours expiry. +url = client.presigned_get_object( + "my-bucket", "my-object", expires=timedelta(hours=2), +) +print(url) diff --git a/examples/presigned_put_object.py b/examples/presigned_put_object.py index c2c7fdf0b..bcfdfb289 100644 --- a/examples/presigned_put_object.py +++ b/examples/presigned_put_object.py @@ -14,24 +14,24 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and my-objectname -# are dummy values, please replace them with original values. - -import datetime +from datetime import timedelta from minio import Minio -from minio.error import ResponseError -client = Minio('s3.amazonaws.com', - access_key='YOUR-ACCESSKEYID', - secret_key='YOUR-SECRETACCESSKEY') +client = Minio( + "play.min.io", + access_key="Q3AM3UQ867SPQQA43P2F", + secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", +) + +# Get presigned URL string to upload data to 'my-object' in +# 'my-bucket' with default expiry (i.e. 7 days). +url = client.presigned_put_object("my-bucket", "my-object") +print(url) -# presigned Put object URL for an object name, expires in 3 days. -try: - print(client.presigned_put_object('my-bucketname', - 'my-objectname', - datetime.timedelta(days=3))) -# Response error is still possible since internally presigned does get -# bucket location. -except ResponseError as err: - print(err) +# Get presigned URL string to upload data to 'my-object' in +# 'my-bucket' with two hours expiry. +url = client.presigned_put_object( + "my-bucket", "my-object", expires=timedelta(hours=2), +) +print(url) diff --git a/minio/api.py b/minio/api.py index 2455222c6..d39bcc5d1 100644 --- a/minio/api.py +++ b/minio/api.py @@ -1915,14 +1915,10 @@ def remove_objects(self, bucket_name, delete_object_list, if error.code != "NoSuchVersion": yield error - def presigned_url(self, method, - bucket_name, - object_name, - expires=timedelta(days=7), - response_headers=None, - request_date=None, - version_id=None, - extra_query_params=None): + def get_presigned_url(self, method, bucket_name, object_name, + expires=timedelta(days=7), response_headers=None, + request_date=None, version_id=None, + extra_query_params=None): """ Get presigned URL of an object for HTTP method, expiry time and custom request parameters. @@ -1942,35 +1938,13 @@ def presigned_url(self, method, :return: URL string. Example:: - # Get presigned URL string to delete 'my-objectname' in - # 'my-bucketname' with one day expiry. - url = minio.presigned_url( + # Get presigned URL string to delete 'my-object' in + # 'my-bucket' with one day expiry. + url = client.get_presigned_url( "DELETE", - "my-bucketname", - "my-objectname", - expires=timedelta(days=1), - ) - print(url) - - # Get presigned URL string to upload 'my-objectname' in - # 'my-bucketname' with response-content-type as application/json - # and one day expiry. - url = minio.presigned_url( - "PUT", - "my-bucketname", - "my-objectname", + "my-bucket", + "my-object", expires=timedelta(days=1), - response_headers={"response-content-type": "application/json"}, - ) - print(url) - - # Get presigned URL string to download 'my-objectname' in - # 'my-bucketname' with two hours expiry. - url = minio.presigned_url( - "GET", - "my-bucketname", - "my-objectname", - expires=timedelta(hours=2), ) print(url) """ @@ -2029,19 +2003,19 @@ def presigned_get_object(self, bucket_name, object_name, :return: URL string. Example:: - # Get presigned URL string to download 'my-objectname' in - # 'my-bucketname' with default expiry. - url = minio.presigned_get_object("my-bucketname", "my-objectname") + # Get presigned URL string to download 'my-object' in + # 'my-bucket' with default expiry (i.e. 7 days). + url = client.presigned_get_object("my-bucket", "my-object") print(url) - # Get presigned URL string to download 'my-objectname' in - # 'my-bucketname' with two hours expiry. - url = minio.presigned_get_object( - "my-bucketname", "my-objectname", expires=timedelta(hours=2), + # Get presigned URL string to download 'my-object' in + # 'my-bucket' with two hours expiry. + url = client.presigned_get_object( + "my-bucket", "my-object", expires=timedelta(hours=2), ) print(url) """ - return self.presigned_url( + return self.get_presigned_url( "GET", bucket_name, object_name, @@ -2064,22 +2038,21 @@ def presigned_put_object(self, bucket_name, object_name, :return: URL string. Example:: - # Get presigned URL string to upload data to 'my-objectname' in - # 'my-bucketname' with default expiry. - url = minio.presigned_put_object("my-bucketname", "my-objectname") + # Get presigned URL string to upload data to 'my-object' in + # 'my-bucket' with default expiry (i.e. 7 days). + url = client.presigned_put_object("my-bucket", "my-object") print(url) - # Get presigned URL string to upload data to 'my-objectname' in - # 'my-bucketname' with two hours expiry. - url = minio.presigned_put_object( - "my-bucketname", "my-objectname", expires=timedelta(hours=2), + # Get presigned URL string to upload data to 'my-object' in + # 'my-bucket' with two hours expiry. + url = client.presigned_put_object( + "my-bucket", "my-object", expires=timedelta(hours=2), ) print(url) """ - return self.presigned_url('PUT', - bucket_name, - object_name, - expires) + return self.get_presigned_url( + "PUT", bucket_name, object_name, expires, + ) def presigned_post_policy(self, policy): """