Skip to content

Commit

Permalink
Refactor PostPolicy implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana committed Nov 27, 2020
1 parent d266a15 commit ff58958
Show file tree
Hide file tree
Showing 11 changed files with 458 additions and 352 deletions.
153 changes: 99 additions & 54 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ s3Client = Minio(
)
```

| Bucket operations | Object operations | Presigned operations |
|:------------------------------------------------------------|:----------------------------------------------------------------|:--------------------------------------------------|
| [`make_bucket`](#make_bucket) | [`get_object`](#get_object) | [`presigned_get_object`](#presigned_get_object) |
| [`list_buckets`](#list_buckets) | [`put_object`](#put_object) | [`presigned_put_object`](#presigned_put_object) |
| [`bucket_exists`](#bucket_exists) | [`copy_object`](#copy_object) | [`presigned_post_policy`](#presigned_post_policy) |
| [`remove_bucket`](#remove_bucket) | [`stat_object`](#stat_object) | |
| [`list_objects`](#list_objects) | [`remove_object`](#remove_object) | |
| [`get_bucket_versioning`](#get_bucket_versioning) | [`remove_objects`](#remove_objects) | |
| [`set_bucket_versioning`](#set_bucket_versioning) | [`fput_object`](#fput_object) | |
| [`delete_bucket_replication`](#delete_bucket_replication) | [`fget_object`](#fget_object) | |
| [`get_bucket_replication`](#get_bucket_replication) | [`select_object_content`](#select_object_content) | |
| [`set_bucket_replication`](#set_bucket_replication) | [`delete_object_tags`](#delete_object_tags) | |
| [`delete_bucket_lifecycle`](#delete_bucket_lifecycle) | [`get_object_tags`](#get_object_tags) | |
| [`get_bucket_lifecycle`](#get_bucket_lifecycle) | [`set_object_tags`](#set_object_tags) | |
| [`set_bucket_lifecycle`](#set_bucket_lifecycle) | [`enable_object_legal_hold`](#enable_object_legal_hold) | |
| [`delete_bucket_tags`](#delete_bucket_tags) | [`disable_object_legal_hold`](#disable_object_legal_hold) | |
| [`get_bucket_tags`](#get_bucket_tags) | [`is_object_legal_hold_enabled`](#is_object_legal_hold_enabled) | |
| [`set_bucket_tags`](#set_bucket_tags) | [`get_object_retention`](#get_object_retention) | |
| [`delete_bucket_policy`](#delete_bucket_policy) | [`set_object_retention`](#set_object_retention) | |
| [`get_bucket_policy`](#get_bucket_policy) | | |
| [`set_bucket_policy`](#set_bucket_policy) | | |
| [`delete_bucket_notification`](#delete_bucket_notification) | | |
| [`get_bucket_notification`](#get_bucket_notification) | | |
| [`set_bucket_notification`](#set_bucket_notification) | | |
| [`listen_bucket_notification`](#listen_bucket_notification) | | |
| [`delete_bucket_encryption`](#delete_bucket_encryption) | | |
| [`get_bucket_encryption`](#get_bucket_encryption) | | |
| [`set_bucket_encryption`](#set_bucket_encryption) | | |
| [`delete_object_lock_config`](#delete_object_lock_config) | | |
| [`get_object_lock_config`](#get_object_lock_config) | | |
| [`set_object_lock_config`](#set_object_lock_config) | | |
| Bucket operations | Object operations |
|:------------------------------------------------------------|:----------------------------------------------------------------|
| [`make_bucket`](#make_bucket) | [`get_object`](#get_object) |
| [`list_buckets`](#list_buckets) | [`put_object`](#put_object) |
| [`bucket_exists`](#bucket_exists) | [`copy_object`](#copy_object) |
| [`remove_bucket`](#remove_bucket) | [`stat_object`](#stat_object) |
| [`list_objects`](#list_objects) | [`remove_object`](#remove_object) |
| [`get_bucket_versioning`](#get_bucket_versioning) | [`remove_objects`](#remove_objects) |
| [`set_bucket_versioning`](#set_bucket_versioning) | [`fput_object`](#fput_object) |
| [`delete_bucket_replication`](#delete_bucket_replication) | [`fget_object`](#fget_object) |
| [`get_bucket_replication`](#get_bucket_replication) | [`select_object_content`](#select_object_content) |
| [`set_bucket_replication`](#set_bucket_replication) | [`delete_object_tags`](#delete_object_tags) |
| [`delete_bucket_lifecycle`](#delete_bucket_lifecycle) | [`get_object_tags`](#get_object_tags) |
| [`get_bucket_lifecycle`](#get_bucket_lifecycle) | [`set_object_tags`](#set_object_tags) |
| [`set_bucket_lifecycle`](#set_bucket_lifecycle) | [`enable_object_legal_hold`](#enable_object_legal_hold) |
| [`delete_bucket_tags`](#delete_bucket_tags) | [`disable_object_legal_hold`](#disable_object_legal_hold) |
| [`get_bucket_tags`](#get_bucket_tags) | [`is_object_legal_hold_enabled`](#is_object_legal_hold_enabled) |
| [`set_bucket_tags`](#set_bucket_tags) | [`get_object_retention`](#get_object_retention) |
| [`delete_bucket_policy`](#delete_bucket_policy) | [`set_object_retention`](#set_object_retention) |
| [`get_bucket_policy`](#get_bucket_policy) | [`presigned_get_object`](#presigned_get_object) |
| [`set_bucket_policy`](#set_bucket_policy) | [`presigned_put_object`](#presigned_put_object) |
| [`delete_bucket_notification`](#delete_bucket_notification) | [`presigned_post_policy`](#presigned_post_policy) |
| [`get_bucket_notification`](#get_bucket_notification) | [`get_presigned_url`](#get_presigned_url) |
| [`set_bucket_notification`](#set_bucket_notification) | |
| [`listen_bucket_notification`](#listen_bucket_notification) | |
| [`delete_bucket_encryption`](#delete_bucket_encryption) | |
| [`get_bucket_encryption`](#get_bucket_encryption) | |
| [`set_bucket_encryption`](#set_bucket_encryption) | |
| [`delete_object_lock_config`](#delete_object_lock_config) | |
| [`get_object_lock_config`](#get_object_lock_config) | |
| [`set_object_lock_config`](#set_object_lock_config) | |

## 1. Constructor

Expand Down Expand Up @@ -1338,8 +1338,6 @@ config = Retention(GOVERNANCE, datetime.utcnow() + timedelta(days=10))
minio.set_object_retention("my-bucketname", "my-objectname", config)
```

## 4. Presigned operations

<a name="presigned_get_object"></a>

### presigned_get_object(bucket_name, object_name, expires=timedelta(days=7), response_headers=None, request_date=None, version_id=None, extra_query_params=None)
Expand Down Expand Up @@ -1368,7 +1366,7 @@ __Example__

```py
# Get presigned URL string to download 'my-objectname' in
# 'my-bucketname' with default expiry.
# 'my-bucketname' with default expiry i.e. 7 days.
url = minio.presigned_get_object("my-bucketname", "my-objectname")
print(url)

Expand Down Expand Up @@ -1404,7 +1402,7 @@ __Example__

```py
# Get presigned URL string to upload data to 'my-objectname' in
# 'my-bucketname' with default expiry.
# 'my-bucketname' with default expiry i.e. 7 days.
url = minio.presigned_put_object("my-bucketname", "my-objectname")
print(url)

Expand All @@ -1418,15 +1416,15 @@ print(url)

<a name="presigned_post_policy"></a>

### presigned_post_policy(post_policy)
### presigned_post_policy(policy)

Get form-data of PostPolicy of an object to upload its data using POST method.

__Parameters__

| Param | Type | Description |
|:--------------|:-------------|:-------------|
| `post_policy` | _PostPolicy_ | Post policy. |
| Param | Type | Description |
|:---------|:-------------|:-------------|
| `policy` | _PostPolicy_ | Post policy. |

__Return Value__

Expand All @@ -1436,27 +1434,74 @@ __Return Value__

__Example__

Create policy:

```py
post_policy = PostPolicy()
post_policy.set_bucket_name('bucket_name')
policy = PostPolicy(
"bucket_name", datetime.utcnow() + timedelta(days=10),
)
policy.add_starts_with_condition("key", "objectPrefix/")
form_data = presigned_post_policy(policy)
print(form_data)
```

# set object prefix for object upload.
post_policy.set_key_startswith('objectPrefix/')
<a name="get_presigned_url"></a>

# set expiry to 10 days.
expires_date = datetime.utcnow()+timedelta(days=10)
post_policy.set_expires(expires_date)
### 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)

# set content length for incoming uploads.
post_policy.set_content_length_range(10, 1024)
Get presigned URL of an object for HTTP method, expiry time and custom request parameters.

# set content-type to allow only text.
post_policy.set_content_type('text/plain')
__Parameters__

form_data = presigned_post_policy(post_policy)
print(form_data)
| 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-objectname' in
# 'my-bucketname' with one day expiry.
url = minio.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.get_presigned_url(
"PUT",
"my-bucketname",
"my-objectname",
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.get_presigned_url(
"GET",
"my-bucketname",
"my-objectname",
expires=timedelta(hours=2),
)
print(url)
```

## 5. Explore Further
Expand Down
60 changes: 60 additions & 0 deletions examples/get_presigned_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- 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.

# 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

client = Minio(
"s3.amazonaws.com",
access_key="YOUR-ACCESSKEYID",
secret_key="YOUR-SECRETACCESSKEY",
)

# Get presigned URL string to delete 'my-objectname' in
# 'my-bucketname' 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 = client.get_presigned_url(
"PUT",
"my-bucketname",
"my-objectname",
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 = client.get_presigned_url(
"GET",
"my-bucketname",
"my-objectname",
expires=timedelta(hours=2),
)
print(url)
29 changes: 18 additions & 11 deletions examples/presigned_get_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@
# 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(
"s3.amazonaws.com",
access_key="YOUR-ACCESSKEYID",
secret_key="YOUR-SECRETACCESSKEY",
)

# Get presigned URL string to download 'my-objectname' in
# 'my-bucketname' with default expiry (i.e. 7 days).
url = client.presigned_get_object("my-bucketname", "my-objectname")
print(url)

# 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-objectname' in
# 'my-bucketname' with two hours expiry.
url = client.presigned_get_object(
"my-bucketname", "my-objectname", expires=timedelta(hours=2),
)
print(url)
57 changes: 25 additions & 32 deletions examples/presigned_post_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,28 @@

from datetime import datetime, timedelta

from minio import Minio, PostPolicy
from minio.error import ResponseError

post_policy = PostPolicy()
# set bucket name location for uploads.
post_policy.set_bucket_name('my-bucketname')
# set key prefix for all incoming uploads.
post_policy.set_key_startswith('my-objectname')
# set content length for incoming uploads.
post_policy.set_content_length_range(10, 1024)

# set expiry 10 days into future.
expires_date = datetime.utcnow() + timedelta(days=10)
post_policy.set_expires(expires_date)

client = Minio('s3.amazonaws.com',
access_key='YOUR-ACCESSKEYID',
secret_key='YOUR-SECRETACCESSKEY')

try:
url, signed_form_data = client.presigned_post_policy(post_policy)

curl_cmd = (
['curl -X POST {0}'.format(url)] +
['-F {0}={1}'.format(k, v) for k, v in signed_form_data.items()] +
['-F file=@<FILE>']
)

# print curl command to upload files.
print(' '.join(curl_cmd))
except ResponseError as err:
print(err)
from minio import Minio
from minio.datatypes import PostPolicy

client = Minio(
"s3.amazonaws.com",
access_key="YOUR-ACCESSKEYID",
secret_key="YOUR-SECRETACCESSKEY",
)

policy = PostPolicy(
"bucket_name", datetime.utcnow() + timedelta(days=10),
)
policy.add_starts_with_condition("key", "objectPrefix/")
policy.add_content_length_range_condition(1*1024*1024, 10*1024*1024)

form_data = client.presigned_post_policy(policy)

curl_cmd = (
"curl -X POST "
"https://s3.amazonaws.com/bucket_name "
"{0} -F file=@<FILE>"
).format(
" ".join(["-F {0}={1}".format(k, v) for k, v in form_data.items()]),
)
print(curl_cmd)
Loading

0 comments on commit ff58958

Please sign in to comment.