Skip to content

Commit 4880ef3

Browse files
committed
added typescript example for deleting an object on oci
1 parent 5145946 commit 4880ef3

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

examples/typescript/delete.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
4+
*/
5+
6+
import common = require("oci-common");
7+
import { ObjectStorageClient, requests } from "oci-objectstorage";
8+
import { Region } from "oci-common";
9+
10+
const configurationFilePath = '~/.oci/config';
11+
const configProfile = "DEFAULT";
12+
13+
const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider(
14+
configurationFilePath,
15+
configProfile
16+
);
17+
/*
18+
* This Sample take filename as a commandline argument and
19+
* deltes the files present in te directory to objectstorage using delete object.
20+
*
21+
* @param args Arguments to provide to the example. The following arguments are expected:
22+
* <ul>
23+
* <li>The first argument is the filename to delete the object from the oracle cloud storage.</li>
24+
* <li>The second argument is the namespaceName</li>
25+
* <li>The third argument is the name of an existing bucket to uplod object</li>
26+
* </ul>
27+
*/
28+
29+
const args = process.argv.slice(2);
30+
console.log(args);
31+
if (args.length !== 3) {
32+
console.error(
33+
"Unexpected number of arguments received. Please pass absloute directory path to read files from"
34+
);
35+
process.exit(-1);
36+
}
37+
38+
const fileName: string = args[0]; // for eg : "download.jpg";
39+
const namespaceName = args[1];
40+
const bucketName = args[2];
41+
42+
const client = new ObjectStorageClient({ authenticationDetailsProvider: provider });
43+
client.region = Region.US_PHOENIX_1;
44+
45+
(async () => {
46+
try {
47+
// build delete object request
48+
const deleteObjectRequest: requests.DeleteObjectRequest = {
49+
bucketName: bucketName,
50+
namespaceName: namespaceName,
51+
objectName: fileName
52+
};
53+
// delete object
54+
const resp = await client.deleteObject(deleteObjectRequest);
55+
console.log(resp);
56+
console.log(`Deleted ${fileName}.`);
57+
} catch (ex) {
58+
console.error(`Failed due to ${ex}`);
59+
}
60+
})();

0 commit comments

Comments
 (0)