diff --git a/docs/API.md b/docs/API.md index 7170998f..bd14d1f8 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2021,7 +2021,7 @@ listener.on('notification', function (record) { -### getBucketPolicy(bucketName [, callback]) +### async getBucketPolicy(bucketName: string): Promise Get the bucket policy associated with the specified bucket. If `objectPrefix` is not empty, the bucket policy will be filtered based on object permissions @@ -2029,18 +2029,15 @@ 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}`) ``` diff --git a/docs/zh_CN/API.md b/docs/zh_CN/API.md index 7d7d8c17..f7fae5ed 100644 --- a/docs/zh_CN/API.md +++ b/docs/zh_CN/API.md @@ -950,7 +950,7 @@ listener.on('notification', function(record) { ``` -### getBucketPolicy(bucketName, objectPrefix[, callback]) +### async getBucketPolicy(bucketName: string): Promise 获取指定存储桶的访问策略,如果`objectPrefix`不为空,则会取相应对象前缀上的访问策略。 @@ -960,18 +960,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}`) ``` diff --git a/src/internal/client.ts b/src/internal/client.ts index 41cd3413..3157a94d 100644 --- a/src/internal/client.ts +++ b/src/internal/client.ts @@ -1786,6 +1786,21 @@ export class TypedClient { await this.makeRequestAsyncOmit({ method, bucketName, query }, policy, [204], '') } + /** + * Get the policy on a bucket or an object prefix. + */ + async getBucketPolicy(bucketName: string): Promise { + // 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 { if (!isValidBucketName(bucketName)) { throw new errors.InvalidBucketNameError(`Invalid bucket name: ${bucketName}`) diff --git a/src/minio.d.ts b/src/minio.d.ts index 590a2f19..73845c34 100644 --- a/src/minio.d.ts +++ b/src/minio.d.ts @@ -417,9 +417,6 @@ export class Client extends TypedClient { removeAllBucketNotification(bucketName: string, callback: NoResultCallback): void removeAllBucketNotification(bucketName: string): Promise - getBucketPolicy(bucketName: string, callback: ResultCallback): void - getBucketPolicy(bucketName: string): Promise - listenBucketNotification( bucketName: string, prefix: string, diff --git a/src/minio.js b/src/minio.js index b0a698d1..0faa6cdb 100644 --- a/src/minio.js +++ b/src/minio.js @@ -796,37 +796,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()) - }) - }) - } - // Generate a generic presigned URL which can be // used for HTTP methods GET, PUT, HEAD and DELETE // @@ -1773,7 +1742,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.removeIncompleteUpload = promisify(Client.prototype.removeIncompleteUpload) Client.prototype.setBucketTagging = promisify(Client.prototype.setBucketTagging) Client.prototype.removeBucketTagging = promisify(Client.prototype.removeBucketTagging) @@ -1810,6 +1778,7 @@ Client.prototype.getBucketTagging = callbackify(Client.prototype.getBucketTaggin Client.prototype.getObjectTagging = callbackify(Client.prototype.getObjectTagging) Client.prototype.setObjectLockConfig = callbackify(Client.prototype.setObjectLockConfig) Client.prototype.getObjectLockConfig = callbackify(Client.prototype.getObjectLockConfig) +Client.prototype.getBucketPolicy = callbackify(Client.prototype.getBucketPolicy) Client.prototype.setBucketPolicy = callbackify(Client.prototype.setBucketPolicy) Client.prototype.getBucketVersioning = callbackify(Client.prototype.getBucketVersioning) Client.prototype.setBucketVersioning = callbackify(Client.prototype.setBucketVersioning)