Skip to content

Commit

Permalink
refactor presigned apis to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx committed Jun 13, 2024
1 parent 06758a5 commit 4d3af8f
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 442 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ The complete API Reference is available here:

### Presigned Operations

- [`presignedUrl`](https://min.io/docs/minio/linux/developers/javascript/API.html#presignedUrl)
- [`presignedGetObject`](https://min.io/docs/minio/linux/developers/javascript/API.html#presignedGetObject)
- [`presignedPutObject`](https://min.io/docs/minio/linux/developers/javascript/API.html#presignedPutObject)
- [`presignedPostPolicy`](https://min.io/docs/minio/linux/developers/javascript/API.html#presignedPostPolicy)
Expand Down Expand Up @@ -242,9 +243,9 @@ The complete API Reference is available here:

#### Presigned Operations

- [presigned-getobject.js](https://github.com/minio/minio-js/blob/master/examples/presigned-getobject.js)
- [presigned-putobject.js](https://github.com/minio/minio-js/blob/master/examples/presigned-putobject.js)
- [presigned-postpolicy.js](https://github.com/minio/minio-js/blob/master/examples/presigned-postpolicy.js)
- [presigned-getobject.mjs](https://github.com/minio/minio-js/blob/master/examples/presigned-getobject.js)
- [presigned-putobject.mjs](https://github.com/minio/minio-js/blob/master/examples/presigned-putobject.js)
- [presigned-postpolicy.mjs](https://github.com/minio/minio-js/blob/master/examples/presigned-postpolicy.js)

#### Bucket Notification Operations

Expand Down
6 changes: 3 additions & 3 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ mc ls play/europetrip/
* [stat-object.mjs](https://github.com/minio/minio-js/blob/master/examples/stat-object.mjs)

#### 完整示例 : Presigned操作
* [presigned-getobject.js](https://github.com/minio/minio-js/blob/master/examples/presigned-getobject.js)
* [presigned-putobject.js](https://github.com/minio/minio-js/blob/master/examples/presigned-putobject.js)
* [presigned-postpolicy.js](https://github.com/minio/minio-js/blob/master/examples/presigned-postpolicy.js)
* [presigned-getobject.mjs](https://github.com/minio/minio-js/blob/master/examples/presigned-getobject.js)
* [presigned-putobject.mjs](https://github.com/minio/minio-js/blob/master/examples/presigned-putobject.js)
* [presigned-postpolicy.mjs](https://github.com/minio/minio-js/blob/master/examples/presigned-postpolicy.js)

#### 完整示例 : 存储桶通知
* [get-bucket-notification.js](https://github.com/minio/minio-js/blob/master/examples/get-bucket-notification.js)
Expand Down
125 changes: 46 additions & 79 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1634,30 +1634,27 @@ Presigned URLs are generated for temporary download/upload access to private obj

<a name="presignedUrl"></a>

### presignedUrl(httpMethod, bucketName, objectName[, expiry, reqParams, requestDate, cb])
### presignedUrl(httpMethod, bucketName, objectName[, expiry, reqParams, requestDate])

Generates a presigned URL for the provided HTTP method, 'httpMethod'. Browsers/Mobile clients may point to this URL to directly download objects even if the bucket is private. This presigned URL can have an associated expiration time in seconds after which the URL is no longer valid. The default value is 7 days.

**Parameters**

| Param | Type | Description |
| ----------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `expiry` | _number_ | Expiry time in seconds. Default value is 7 days. (optional) |
| `reqParams` | _object_ | request parameters. (optional) e.g {versionId:"10fa9946-3f64-4137-a58f-888065c0732e"} |
| `requestDate` | _Date_ | A date object, the url will be issued at. Default value is now. (optional) |
| `callback(err, presignedUrl)` | _function_ | Callback function is called with non `null` err value in case of error. `presignedUrl` will be the URL using which the object can be downloaded using GET request. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------- | -------- | ------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `expiry` | _number_ | Expiry time in seconds. Default value is 7 days. (optional) |
| `reqParams` | _object_ | request parameters. (optional) e.g {versionId:"10fa9946-3f64-4137-a58f-888065c0732e"} |
| `requestDate` | _Date_ | A date object, the url will be issued at. Default value is now. (optional) |

**Example1**

```js
// presigned url for 'getObject' method.
// expires in a day.
minioClient.presignedUrl('GET', 'mybucket', 'hello.txt', 24 * 60 * 60, function (err, presignedUrl) {
if (err) return console.log(err)
console.log(presignedUrl)
})
const presignedUrl = await minioClient.presignedUrl('GET', 'mybucket', 'hello.txt', 24 * 60 * 60)
console.log(presignedUrl)
```

**Example2**
Expand All @@ -1666,100 +1663,73 @@ minioClient.presignedUrl('GET', 'mybucket', 'hello.txt', 24 * 60 * 60, function
// presigned url for 'listObject' method.
// Lists objects in 'myBucket' with prefix 'data'.
// Lists max 1000 of them.
minioClient.presignedUrl(
'GET',
'mybucket',
'',
1000,
{ prefix: 'data', 'max-keys': 1000 },
function (err, presignedUrl) {
if (err) return console.log(err)
console.log(presignedUrl)
},
)
await minioClient.presignedUrl('GET', 'mybucket', '', 1000, { prefix: 'data', 'max-keys': 1000 })
```

**Example 3**

```js
// Get Object with versionid
minioClient.presignedUrl(
'GET',
'mybucket',
'',
1000,
{ versionId: '10fa9946-3f64-4137-a58f-888065c0732e' },
function (err, presignedUrl) {
if (err) return console.log(err)
console.log(presignedUrl)
},
)
await minioClient.presignedUrl('GET', 'mybucket', '', 1000, { versionId: '10fa9946-3f64-4137-a58f-888065c0732e' })
```

<a name="presignedGetObject"></a>

### presignedGetObject(bucketName, objectName[, expiry, respHeaders, requestDate, cb])
### presignedGetObject(bucketName, objectName[, expiry, respHeaders, requestDate])

Generates a presigned URL for HTTP GET operations. Browsers/Mobile clients may point to this URL to directly download objects even if the bucket is private. This presigned URL can have an associated expiration time in seconds after which the URL is no longer valid. The default value is 7 days.

**Parameters**

| Param | Type | Description |
| ----------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `expiry` | _number_ | Expiry time in seconds. Default value is 7 days. (optional) |
| `respHeaders` | _object_ | response headers to override (optional) |
| `requestDate` | _Date_ | A date object, the url will be issued at. Default value is now. (optional) |
| `callback(err, presignedUrl)` | _function_ | Callback function is called with non `null` err value in case of error. `presignedUrl` will be the URL using which the object can be downloaded using GET request. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------- | -------- | -------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `expiry` | _number_ | Expiry time in seconds. Default value is 7 days. (optional) |
| `respHeaders` | _object_ | response headers to override (optional) |
| `requestDate` | _Date_ | A date object, the url will be issued at. Default value is now. (optional) |

**Example**

```js
// expires in a day.
minioClient.presignedGetObject('mybucket', 'hello.txt', 24 * 60 * 60, function (err, presignedUrl) {
if (err) return console.log(err)
console.log(presignedUrl)
})
const presignedUrl = await minioClient.presignedGetObject('mybucket', 'hello.txt', 24 * 60 * 60)
console.log(presignedUrl)
```

<a name="presignedPutObject"></a>

### presignedPutObject(bucketName, objectName, expiry[, callback])
### presignedPutObject(bucketName, objectName [,expiry])

Generates a presigned URL for HTTP PUT operations. Browsers/Mobile clients may point to this URL to upload objects directly to a bucket even if it is private. This presigned URL can have an associated expiration time in seconds after which the URL is no longer valid. The default value is 7 days.

**Parameters**

| Param | Type | Description |
| ----------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `expiry` | _number_ | Expiry time in seconds. Default value is 7 days. |
| `callback(err, presignedUrl)` | _function_ | Callback function is called with non `null` err value in case of error. `presignedUrl` will be the URL using which the object can be uploaded using PUT request. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------ | -------- | ------------------------------------------------ |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `expiry` | _number_ | Expiry time in seconds. Default value is 7 days. |

**Example**

```js
// expires in a day.
minioClient.presignedPutObject('mybucket', 'hello.txt', 24 * 60 * 60, function (err, presignedUrl) {
if (err) return console.log(err)
console.log(presignedUrl)
})
const presignedUrl = await minioClient.presignedPutObject('mybucket', 'hello.txt', 24 * 60 * 60)
console.log(presignedUrl)
```

<a name="presignedPostPolicy"></a>

### presignedPostPolicy(policy[, callback])
### presignedPostPolicy(policy)

Allows setting policy conditions to a presigned URL for POST operations. Policies such as bucket name to receive object uploads, key name prefixes, expiry policy may be set.

**Parameters**

| Param | Type | Description |
| ------------------------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `policy` | _object_ | Policy object created by minioClient.newPostPolicy() |
| `callback(err, {postURL, formData})` | _function_ | Callback function is called with non `null` err value in case of error. `postURL` will be the URL using which the object can be uploaded using POST request. `formData` is the object having key/value pairs for the Form data of POST body. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| -------- | -------- | ---------------------------------------------------- |
| `policy` | _object_ | Policy object created by minioClient.newPostPolicy() |

Create policy:

Expand Down Expand Up @@ -1806,23 +1776,20 @@ policy.setUserMetaData({
POST your content from the browser using `superagent`:

```js
minioClient.presignedPostPolicy(policy, function (err, data) {
if (err) return console.log(err)

const req = superagent.post(data.postURL)
_.each(data.formData, function (value, key) {
req.field(key, value)
})
const { postURL, formData } = minioClient.presignedPostPolicy(policy)
const req = superagent.post(postURL)
_.each(formData, function (value, key) {
req.field(key, value)
})

// file contents.
req.attach('file', '/path/to/hello.txt', 'hello.txt')
// file contents.
req.attach('file', '/path/to/hello.txt', 'hello.txt')

req.end(function (err, res) {
if (err) {
return console.log(err.toString())
}
console.log('Upload successful.')
})
req.end(function (err, res) {
if (err) {
return console.log(err.toString())
}
console.log('Upload successful.')
})
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,15 @@ const s3Client = new Minio.Client({
const requestDate = new Date()

requestDate.setHours(0, 0, 0, 0)
s3Client.presignedGetObject('my-bucketname', 'my-objectname', 1000, {}, requestDate, function (e, presignedUrl) {
if (e) {
return console.log(e)
}
console.log(presignedUrl)
})
const presignedUrl = await s3Client.presignedGetObject('my-bucketname', 'my-objectname', 1000, {}, requestDate)
console.log(presignedUrl)

// Versioning support
s3Client.presignedGetObject(
const presignedUrlV = s3Client.presignedGetObject(
'my-bucketname',
'my-objectname',
1000,
{ versionId: '10fa9946-3f64-4137-a58f-888065c0732e' },
requestDate,
function (e, presignedUrl) {
if (e) {
return console.log(e)
}
console.log(presignedUrl)
},
)
console.log(presignedUrlV)
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@ const s3Client = new Minio.Client({
})

// Presigned get object URL for my-objectname at my-bucketname, it expires in 7 days by default.
s3Client.presignedGetObject('my-bucketname', 'my-objectname', 1000, function (e, presignedUrl) {
if (e) {
return console.log(e)
}
console.log(presignedUrl)
})
const presignedUrl = await s3Client.presignedGetObject('my-bucketname', 'my-objectname', 1000)
console.log(presignedUrl)
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ policy.setUserMetaData({
key: 'value',
})

s3Client.presignedPostPolicy(policy, function (e, data) {
if (e) {
return console.log(e)
}
const curl = []
curl.push(`curl ${data.postURL}`)
for (const [key, value] of Object.entries(data.formData)) {
curl.push(`-F ${key}=${value}`)
}
// Print curl command to upload files.
curl.push('-F file=@<FILE>')
console.log(curl.join(' '))
})
const { postURL, formData } = await s3Client.presignedPostPolicy(policy)
const curl = []
curl.push(`curl ${postURL}`)
for (const [key, value] of Object.entries(formData)) {
curl.push(`-F ${key}=${value}`)
}
// Print curl command to upload files.
curl.push('-F file=@<FILE>')
console.log(curl.join(' '))
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@ const s3Client = new Minio.Client({
useSSL: true, // Default is true.
})

s3Client.presignedPutObject('my-bucketname', 'my-objectname', 1000, function (e, presignedUrl) {
if (e) {
return console.log(e)
}
console.log(presignedUrl)
})
const presignedUrl = await s3Client.presignedPutObject('my-bucketname', 'my-objectname', 1000)
console.log(presignedUrl)
2 changes: 2 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export { ENCRYPTION_TYPES, LEGAL_HOLD_STATUS, RETENTION_MODES, RETENTION_VALIDIT

export const DEFAULT_REGION = 'us-east-1'

export const PRESIGN_EXPIRY_DAYS_MAX = 24 * 60 * 60 * 7 // 7 days in seconds

export interface ICopySourceOptions {
Bucket: string
Object: string
Expand Down
Loading

0 comments on commit 4d3af8f

Please sign in to comment.