Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: getBucketPolicy #1224

Merged
merged 12 commits into from
Dec 6, 2023
15 changes: 6 additions & 9 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2026,26 +2026,23 @@ listener.on('notification', function (record) {

<a name="getBucketPolicy"></a>

### getBucketPolicy(bucketName [, callback])
### async getBucketPolicy(bucketName: string): Promise<string>

Get the bucket policy associated with the specified bucket. If `objectPrefix`
is not empty, the bucket policy will be filtered based on object permissions
as well.

**Parameters**

| Param | Type | Description |
| ----------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket |
| `callback(err, policy)` | _function_ | Callback function is called with non `null` err value in case of error. `policy` is [bucket policy](https://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html). If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------ | -------- | ------------------ |
| `bucketName` | _string_ | Name of the bucket |

```js
// Retrieve bucket policy of 'my-bucketname'
minioClient.getBucketPolicy('my-bucketname', function (err, policy) {
if (err) throw err
const policy = await minioClient.getBucketPolicy('my-bucketname')

console.log(`Bucket policy file: ${policy}`)
})
console.log(`Bucket policy file: ${policy}`)
```

<a name="setBucketPolicy"></a>
Expand Down
11 changes: 3 additions & 8 deletions docs/zh_CN/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ listener.on('notification', function(record) {
```

<a name="getBucketPolicy"></a>
### getBucketPolicy(bucketName, objectPrefix[, callback])
### async getBucketPolicy(bucketName: string): Promise<string>

获取指定存储桶的访问策略,如果`objectPrefix`不为空,则会取相应对象前缀上的访问策略。

Expand All @@ -968,18 +968,13 @@ __参数__
| 参数 | 类型 | 描述 |
|---|---|---|
| `bucketName` | _string_ | 存储桶名称。 |
| `objectPrefix` | _string_ | 用于过滤的对象前缀,`''`代表整个存储桶。 |
| `callback(err, policy)` | _function_ | 如果`err`不是null则代表有错误。`policy`是存储桶策略的字符串表示(`minio.Policy.NONE`,`minio.Policy.READONLY`,`minio.Policy.WRITEONLY`,或者`minio.Policy.READWRITE`). 如果没有传callback的话,则返回一个`Promise`对象。 |


```js
// Retrieve bucket policy of 'my-bucketname' that applies to all objects that
// start with 'img-'.
minioClient.getBucketPolicy('my-bucketname', 'img-', function(err, policy) {
if (err) throw err
const policy = await minioClient.getBucketPolicy('my-bucketname')

console.log(`Bucket policy: ${policy}`)
})
console.log(`Bucket policy file: ${policy}`)
```

<a name="setBucketPolicy"></a>
Expand Down
15 changes: 15 additions & 0 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@

const chunkier = new BlockStream2({ size: partSize, zeroPadding: false })

const [_, o] = await Promise.all([

Check warning on line 1503 in src/internal/client.ts

View workflow job for this annotation

GitHub Actions / lint

'_' is assigned a value but never used
new Promise((resolve, reject) => {
body.pipe(chunkier).on('error', reject)
chunkier.on('end', resolve).on('error', reject)
Expand Down Expand Up @@ -1743,6 +1743,21 @@
return xmlParsers.parseTagging(body)
}

/**
* Get the policy on a bucket or an object prefix.
*/
async getBucketPolicy(bucketName: string): Promise<string> {
// Validate arguments.
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError(`Invalid bucket name: ${bucketName}`)
}

const method = 'GET'
const query = 'policy'
const res = await this.makeRequestAsync({ method, bucketName, query })
return await readAsString(res)
}

async putObjectRetention(bucketName: string, objectName: string, retentionOpts: Retention = {}): Promise<void> {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError(`Invalid bucket name: ${bucketName}`)
Expand Down
3 changes: 0 additions & 3 deletions src/minio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,6 @@ export class Client extends TypedClient {
removeAllBucketNotification(bucketName: string, callback: NoResultCallback): void
removeAllBucketNotification(bucketName: string): Promise<void>

getBucketPolicy(bucketName: string, callback: ResultCallback<string>): void
getBucketPolicy(bucketName: string): Promise<string>

setBucketPolicy(bucketName: string, bucketPolicy: string, callback: NoResultCallback): void
setBucketPolicy(bucketName: string, bucketPolicy: string): Promise<void>

Expand Down
34 changes: 1 addition & 33 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,37 +820,6 @@ export class Client extends TypedClient {
)
}

// Get the policy on a bucket or an object prefix.
//
// __Arguments__
// * `bucketName` _string_: name of the bucket
// * `callback(err, policy)` _function_: callback function
getBucketPolicy(bucketName, cb) {
// Validate arguments.
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError(`Invalid bucket name: ${bucketName}`)
}
if (!isFunction(cb)) {
throw new TypeError('callback should be of type "function"')
}

let method = 'GET'
let query = 'policy'
this.makeRequest({ method, bucketName, query }, '', [200], '', true, (e, response) => {
if (e) {
return cb(e)
}

let policy = Buffer.from('')
pipesetup(response, transformers.getConcater())
.on('data', (data) => (policy = data))
.on('error', cb)
.on('end', () => {
cb(null, policy.toString())
})
})
}

// Set the policy on a bucket or an object prefix.
//
// __Arguments__
Expand Down Expand Up @@ -1827,7 +1796,6 @@ Client.prototype.presignedPostPolicy = promisify(Client.prototype.presignedPostP
Client.prototype.getBucketNotification = promisify(Client.prototype.getBucketNotification)
Client.prototype.setBucketNotification = promisify(Client.prototype.setBucketNotification)
Client.prototype.removeAllBucketNotification = promisify(Client.prototype.removeAllBucketNotification)
Client.prototype.getBucketPolicy = promisify(Client.prototype.getBucketPolicy)
Client.prototype.setBucketPolicy = promisify(Client.prototype.setBucketPolicy)
Client.prototype.removeIncompleteUpload = promisify(Client.prototype.removeIncompleteUpload)
Client.prototype.setBucketTagging = promisify(Client.prototype.setBucketTagging)
Expand Down Expand Up @@ -1864,6 +1832,6 @@ Client.prototype.getObjectTagging = callbackify(Client.prototype.getObjectTaggin
Client.prototype.putObjectRetention = callbackify(Client.prototype.putObjectRetention)
Client.prototype.setObjectLockConfig = callbackify(Client.prototype.setObjectLockConfig)
Client.prototype.getObjectLockConfig = callbackify(Client.prototype.getObjectLockConfig)

Client.prototype.getBucketPolicy = callbackify(Client.prototype.getBucketPolicy)
Client.prototype.getBucketVersioning = callbackify(Client.prototype.getBucketVersioning)
Client.prototype.setBucketVersioning = callbackify(Client.prototype.setBucketVersioning)
Loading