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

migrate removeIncompleteUpload api to ts #1290

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1261,27 +1261,21 @@ objectsStream.on('end', async () => {

<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
prakashsvmx marked this conversation as resolved.
Show resolved Hide resolved
// are dummy values, please replace them with original values.
prakashsvmx marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -2453,4 +2453,17 @@ export class TypedClient {
const batchResults = await Promise.all(batches.map(runDeleteObjects))
return batchResults.flat()
}

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])
}
}
36 changes: 1 addition & 35 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,40 +85,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 @@ -1022,7 +988,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 @@ -1066,3 +1031,4 @@ Client.prototype.getBucketEncryption = callbackify(Client.prototype.getBucketEnc
Client.prototype.removeBucketEncryption = callbackify(Client.prototype.removeBucketEncryption)
Client.prototype.getObjectRetention = callbackify(Client.prototype.getObjectRetention)
Client.prototype.removeObjects = callbackify(Client.prototype.removeObjects)
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 @@ -793,40 +793,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
Loading