Skip to content

Commit 31faff2

Browse files
authored
Merge pull request #10 from omkarvijay5/more-ts-examples
Examples for renaming objects and deleting objects, and creating pre-authenticated request urls
2 parents b6f5d71 + 18c6d38 commit 31faff2

File tree

6 files changed

+421
-0
lines changed

6 files changed

+421
-0
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
/* @param args Arguments to provide to the example. The following arguments are expected:
7+
/*
8+
* This Sample take directory path as a commandline argument and
9+
* generates urls for the files present in the directory to objectstorage using create preauthenticated request.
10+
*
11+
* @param args Arguments to provide to the example. The following arguments are expected:
12+
* <ul>
13+
* <li>The first argument is the absloute directory path to read files from.</li>
14+
* <li>The second argument is the name of an existing bucket name to generate the url.</li>
15+
* </ul>
16+
*/
17+
18+
const os = require('oci-objectstorage');
19+
const common = require('oci-common');
20+
const { readdir } = require('fs');
21+
22+
const configurationFilePath = '~/.oci/config';
23+
const configProfile = 'DEFAULT';
24+
25+
const provider = new common.ConfigFileAuthenticationDetailsProvider(
26+
configurationFilePath,
27+
configProfile
28+
);
29+
30+
const args = process.argv.slice(2);
31+
if (args.length !== 3) {
32+
console.error(
33+
'Unexpected number of arguments received. Consult the script header comments for expected arguments'
34+
);
35+
process.exit(-1);
36+
}
37+
38+
const filePath = args[0];
39+
const bucketName = args[1];
40+
const namespaceName = args[2];
41+
const serviceName = 'objectstorage'
42+
43+
const client = new os.ObjectStorageClient({
44+
authenticationDetailsProvider: provider
45+
});
46+
client.region = common.Region.US_PHOENIX_1;
47+
48+
(async () => {
49+
try {
50+
console.time(`Download Time ${filePath}`);
51+
52+
// set expiry date for the download url.
53+
const today = new Date();
54+
const neverExpires = new Date(today);
55+
neverExpires.setDate(neverExpires.getDate() + 25);
56+
57+
// use date for generating a random unique id which can be used as a par id
58+
const parUniqueId = Date.now();
59+
const createPreauthenticatedRequestDetails = {
60+
name: parUniqueId.toString(),
61+
objectName: filePath,
62+
accessType: os.models.CreatePreauthenticatedRequestDetails.AccessType.ObjectRead,
63+
timeExpires: neverExpires
64+
};
65+
const createPreauthenticatedRequest = {
66+
bucketName: bucketName,
67+
namespaceName: namespaceName,
68+
createPreauthenticatedRequestDetails: createPreauthenticatedRequestDetails
69+
};
70+
// create pre authenticated request to generate the url
71+
const resp = await client.createPreauthenticatedRequest(createPreauthenticatedRequest);
72+
const baseUrl = `https://${serviceName}.${common.Region.US_PHOENIX_1.regionId}.${common.Realm.OC1.secondLevelDomain}`
73+
const downloadUrl = resp.preauthenticatedRequest.accessUri;
74+
console.log('download url for the file ' + filePath + ' is ' + baseUrl + downloadUrl);
75+
76+
console.timeEnd(`Download Time ${filePath}`);
77+
} catch (error) {
78+
console.log('Error executing example ' + error);
79+
}
80+
81+
})();

examples/javascript/delete.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/*
7+
* This Sample take filename as a commandline argument and
8+
* deltes the files present in te directory to objectstorage using delete object.
9+
*
10+
* @param args Arguments to provide to the example. The following arguments are expected:
11+
* <ul>
12+
* <li>The first argument is the filename to delete the object from the oracle cloud storage.</li>
13+
* <li>The second argument is the namespaceName</li>
14+
* <li>The third argument is the name of an existing bucket to uplod object</li>
15+
* </ul>
16+
*/
17+
18+
const os = require('oci-objectstorage');
19+
const common = require('oci-common');
20+
const { readdir } = require('fs');
21+
22+
const configurationFilePath = '~/.oci/config';
23+
const configProfile = 'DEFAULT';
24+
25+
const provider = new common.ConfigFileAuthenticationDetailsProvider(
26+
configurationFilePath,
27+
configProfile
28+
);
29+
30+
const args = process.argv.slice(2);
31+
if (args.length !== 3) {
32+
console.error(
33+
'Unexpected number of arguments received. Consult the script header comments for expected arguments'
34+
);
35+
process.exit(-1);
36+
}
37+
38+
const fileName = args[0]; // for eg : "download.jpg";
39+
const namespaceName = args[1];
40+
const bucketName = args[2];
41+
42+
43+
const client = new os.ObjectStorageClient({
44+
authenticationDetailsProvider: provider
45+
});
46+
client.region = common.Region.US_PHOENIX_1;
47+
48+
(async () => {
49+
try {
50+
// build delete object request
51+
const deleteObjectRequest = {
52+
bucketName: bucketName,
53+
namespaceName: namespaceName,
54+
objectName: fileName
55+
};
56+
// delete object
57+
const resp = await client.deleteObject(deleteObjectRequest);
58+
console.log(resp);
59+
console.log(`Deleted ${fileName}.`);
60+
} catch (ex) {
61+
console.error(`Failed due to ${ex}`);
62+
}
63+
})();

examples/javascript/rename.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
/*
7+
* This Sample takes filename as a commandline argument and
8+
* rename the file name to the new file name using rename object.
9+
*
10+
* @param args Arguments to provide to the example. The following arguments are expected:
11+
* <ul>
12+
* <li>The first argument is the source file name in the object storage.</li>
13+
* <li>The second argument is the new file name to be renamed.</li>
14+
* <li>The third argument is the namespaceName</li>
15+
* <li>The fourth argument is the name of an existing bucket to rename object</li>
16+
* </ul>
17+
*/
18+
19+
const os = require('oci-objectstorage');
20+
const common = require('oci-common');
21+
22+
const configurationFilePath = '~/.oci/config';
23+
const configProfile = 'DEFAULT';
24+
25+
const provider = new common.ConfigFileAuthenticationDetailsProvider(
26+
configurationFilePath,
27+
configProfile
28+
);
29+
30+
const args = process.argv.slice(2);
31+
if (args.length !== 4) {
32+
console.error(
33+
'Unexpected number of arguments received. Consult the script header comments for expected arguments'
34+
);
35+
process.exit(-1);
36+
}
37+
38+
const sourceName = args[0]; // for eg : "download.jpg";
39+
const newName = args[1]; // for eg : "downloadNew.jpg";
40+
const namespaceName = args[2];
41+
const bucketName = args[3];
42+
43+
const client = new os.ObjectStorageClient({
44+
authenticationDetailsProvider: provider
45+
});
46+
client.region = common.Region.US_PHOENIX_1;
47+
48+
(async () => {
49+
try {
50+
//building the rename object request
51+
const renameObjectDetails = {
52+
sourceName: sourceName,
53+
newName: newName
54+
};
55+
const renameObject = {
56+
namespaceName: namespaceName,
57+
bucketName: bucketName,
58+
renameObjectDetails: renameObjectDetails
59+
};
60+
// rename the file name to the new filename
61+
const resp = await client.renameObject(renameObject);
62+
console.log(resp);
63+
console.log(`Renamed ${sourceName} to ${newName}.`);
64+
} catch (ex) {
65+
console.error(`Failed due to ${ex}`);
66+
console.error(
67+
`The object '${sourceName}' may not exist in bucket '${bucketName}' with namespace '${namespaceName}'`
68+
);
69+
}
70+
71+
})();
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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, models } from "oci-objectstorage";
8+
import { Region } from "oci-common";
9+
import { readdir } from "fs";
10+
11+
const configurationFilePath = "~/.oci/config";
12+
const configProfile = "DEFAULT";
13+
14+
const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider(
15+
configurationFilePath,
16+
configProfile
17+
);
18+
/*
19+
* This Sample take directory path as a commandline argument and
20+
* generates urls for the files present in the directory to objectstorage using create preauthenticated request.
21+
*
22+
* @param args Arguments to provide to the example. The following arguments are expected:
23+
* <ul>
24+
* <li>The first argument is the absloute directory path to read files from.</li>
25+
* <li>The second argument is the name of an existing bucket name to generate the url.</li>
26+
* </ul>
27+
*/
28+
29+
const args = process.argv.slice(2);
30+
if (args.length !== 3) {
31+
console.error(
32+
"Unexpected number of arguments received. Please pass absloute directory path to read files from"
33+
);
34+
process.exit(-1);
35+
}
36+
37+
const filePath: string = args[0]; // for eg : "/Users/Abc/upload-manager";
38+
const bucketName = args[1];
39+
const namespaceName = args[2]
40+
const serviceName = "objectstorage"
41+
42+
const client = new ObjectStorageClient({ authenticationDetailsProvider: provider });
43+
client.region = Region.US_PHOENIX_1;
44+
45+
(async () => {
46+
try {
47+
// creating pre authentication request which generates the download url for the file
48+
console.time(`Download Time ${filePath}`);
49+
50+
// set expiry date for the download url.
51+
const today = new Date();
52+
const neverExpires = new Date(today);
53+
neverExpires.setDate(neverExpires.getDate() + 25);
54+
55+
// use date for generating a random unique id which can be used as a par id
56+
const parUniqueId = Date.now();
57+
const createPreauthenticatedRequestDetails = {
58+
name: parUniqueId.toString(),
59+
objectName: filePath,
60+
accessType: models.CreatePreauthenticatedRequestDetails.AccessType.ObjectRead,
61+
timeExpires: neverExpires
62+
} as models.CreatePreauthenticatedRequestDetails;
63+
const createPreauthenticatedRequest: requests.CreatePreauthenticatedRequestRequest = {
64+
bucketName: bucketName,
65+
namespaceName: namespaceName,
66+
createPreauthenticatedRequestDetails: createPreauthenticatedRequestDetails
67+
};
68+
// create pre authenticated request to generate the url
69+
const resp = await client.createPreauthenticatedRequest(createPreauthenticatedRequest);
70+
const baseUrl = `https://${serviceName}.${common.Region.US_PHOENIX_1.regionId}.${common.Realm.OC1.secondLevelDomain}`
71+
const downloadUrl = resp.preauthenticatedRequest.accessUri;
72+
console.log("download url for the file " + filePath + " is " + baseUrl + downloadUrl);
73+
74+
console.timeEnd(`Download Time ${filePath}`);
75+
} catch (ex) {
76+
console.error(`Failed due to ${ex}`);
77+
}
78+
})();

examples/typescript/delete.ts

+60
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)