Skip to content

Commit

Permalink
migrate removeIncompleteUpload api to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashsvmx committed May 10, 2024
1 parent 6b91636 commit 9103922
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 68 deletions.
18 changes: 6 additions & 12 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1273,27 +1273,21 @@ objectsStream.on('end', function () {

<a name="removeIncompleteUpload"></a>

### removeIncompleteUpload(bucketName, objectName[, callback])
### removeIncompleteUpload(bucketName, objectName)

Removes a partially uploaded object.

**Parameters**

| Param | Type | Description |
| --------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |
| `callback(err)` | _function_ | Callback function is called with non `null` value in case of error. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------ | -------- | ------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `objectName` | _string_ | Name of the object. |

**Example**

```js
minioClient.removeIncompleteUpload('mybucket', 'photo.jpg', function (err) {
if (err) {
return console.log('Unable to remove incomplete object', err)
}
console.log('Incomplete object removed successfully.')
})
await minioClient.removeIncompleteUpload('mybucket', 'photo.jpg')
```

<a name="putObjectRetention"></a>
Expand Down
35 changes: 35 additions & 0 deletions examples/remove-incomplete-upload.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* MinIO Javascript Library for Amazon S3 Compatible Cloud Storage, (C) 2024 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 and my-bucketname
// are dummy values, please replace them with original values.

import * as Minio from 'minio'

const s3Client = new Minio.Client({
endPoint: 's3.amazonaws.com',
accessKey: 'YOUR-ACCESSKEYID',
secretKey: 'YOUR-SECRETACCESSKEY',
})

// Note: It is a no-op in MinIO
// Can be used/tested with AWS S3.
try {
await s3Client.removeIncompleteUpload('test-bucket', 'incomplete-object-name')
console.log('Success')
} catch (e) {
console.log(e)
}
13 changes: 13 additions & 0 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2429,4 +2429,17 @@ export class TypedClient {
const body = await readAsString(res)
return xmlParsers.parseObjectRetentionConfig(body)
}

async removeIncompleteUpload(bucketName: string, objectName: string): Promise<void> {
if (!isValidBucketName(bucketName)) {
throw new errors.IsValidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!isValidObjectName(objectName)) {
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
}
const removeUploadId = await this.findUploadId(bucketName, objectName)
const method = 'DELETE'
const query = `uploadId=${removeUploadId}`
await this.makeRequestAsyncOmit({ method, bucketName, objectName, query }, '', [204])
}
}
2 changes: 0 additions & 2 deletions src/minio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ export class Client extends TypedClient {
removeObjects(bucketName: string, objectsList: string[], callback: NoResultCallback): void
removeObjects(bucketName: string, objectsList: string[]): Promise<void>

removeIncompleteUpload(bucketName: string, objectName: string, callback: NoResultCallback): void
removeIncompleteUpload(bucketName: string, objectName: string): Promise<void>
composeObject(
destObjConfig: CopyDestinationOptions,
sourceObjList: CopySourceOptions[],
Expand Down
35 changes: 1 addition & 34 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,6 @@ export class Client extends TypedClient {
this.userAgent = `${this.userAgent} ${appName}/${appVersion}`
}

// Remove the partially uploaded object.
//
// __Arguments__
// * `bucketName` _string_: name of the bucket
// * `objectName` _string_: name of the object
// * `callback(err)` _function_: callback function is called with non `null` value in case of error
removeIncompleteUpload(bucketName, objectName, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.IsValidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!isValidObjectName(objectName)) {
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
}
if (!isFunction(cb)) {
throw new TypeError('callback should be of type "function"')
}
var removeUploadId
async.during(
(cb) => {
this.findUploadId(bucketName, objectName).then((uploadId) => {
removeUploadId = uploadId
cb(null, uploadId)
}, cb)
},
(cb) => {
var method = 'DELETE'
var query = `uploadId=${removeUploadId}`
this.makeRequest({ method, bucketName, objectName, query }, '', [204], '', false, (e) => cb(e))
},
cb,
)
}

// Copy the object.
//
// __Arguments__
Expand Down Expand Up @@ -1110,7 +1077,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.removeIncompleteUpload = promisify(Client.prototype.removeIncompleteUpload)
Client.prototype.composeObject = promisify(Client.prototype.composeObject)

// refactored API use promise internally
Expand Down Expand Up @@ -1153,3 +1119,4 @@ Client.prototype.setBucketEncryption = callbackify(Client.prototype.setBucketEnc
Client.prototype.getBucketEncryption = callbackify(Client.prototype.getBucketEncryption)
Client.prototype.removeBucketEncryption = callbackify(Client.prototype.removeBucketEncryption)
Client.prototype.getObjectRetention = callbackify(Client.prototype.getObjectRetention)
Client.prototype.removeIncompleteUpload = callbackify(Client.prototype.removeIncompleteUpload)
46 changes: 26 additions & 20 deletions tests/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,40 +788,46 @@ describe('Client', function () {
})

describe('#removeIncompleteUpload(bucket, object, callback)', () => {
it('should fail on null bucket', (done) => {
it('should fail on null bucket', async () => {
try {
client.removeIncompleteUpload(null, 'hello', function () {})
} catch (e) {
done()
await client.removeIncompleteUpload(null, 'hello')
} catch (err) {
return
}
throw new Error('callback should receive error')
})
it('should fail on empty bucket', (done) => {
it('should fail on empty bucket', async () => {
try {
client.removeIncompleteUpload('', 'hello', function () {})
} catch (e) {
done()
await client.removeIncompleteUpload('', 'hello')
} catch (err) {
return
}
throw new Error('callback should receive error')
})
it('should fail on empty bucket', (done) => {
it('should fail on empty bucket', async () => {
try {
client.removeIncompleteUpload(' \n \t ', 'hello', function () {})
} catch (e) {
done()
await client.removeIncompleteUpload(' \n \t ', 'hello')
} catch (err) {
return
}
throw new Error('callback should receive error')
})
it('should fail on null object', (done) => {

it('should fail on null object', async () => {
try {
client.removeIncompleteUpload('hello', null, function () {})
} catch (e) {
done()
await client.removeIncompleteUpload('hello', null)
} catch (err) {
return
}
throw new Error('callback should receive error')
})
it('should fail on empty object', (done) => {
it('should fail on empty object', async () => {
try {
client.removeIncompleteUpload('hello', '', function () {})
} catch (e) {
done()
await client.removeIncompleteUpload('hello', '')
} catch (err) {
return
}
throw new Error('callback should receive error')
})
})
})
Expand Down

0 comments on commit 9103922

Please sign in to comment.