|
12 | 12 | * <ul> |
13 | 13 | * <li>The first argument is the absloute directory path to read files from.</li> |
14 | 14 | * <li>The second argument is the name of an existing bucket name to generate the url.</li> |
| 15 | + * <li>The third argument is the name of the namespace</li> |
| 16 | + * <li>The fourth argument is the regionId, i.e: "us-phoenix-1"</li> |
| 17 | + * <li>The fifth argument is second level domain i.e: "oraclecloud.com"</li> |
15 | 18 | * </ul> |
16 | 19 | */ |
17 | 20 |
|
18 | | -const os = require('oci-objectstorage'); |
19 | | -const common = require('oci-common'); |
20 | | -const { readdir } = require('fs'); |
| 21 | +const os = require("oci-objectstorage"); |
| 22 | +const common = require("oci-common"); |
| 23 | +const { readdir } = require("fs"); |
21 | 24 |
|
22 | | -const configurationFilePath = '~/.oci/config'; |
23 | | -const configProfile = 'DEFAULT'; |
24 | | - |
25 | | -const provider = new common.ConfigFileAuthenticationDetailsProvider( |
26 | | - configurationFilePath, |
27 | | - configProfile |
28 | | -); |
| 25 | +const provider = new common.ConfigFileAuthenticationDetailsProvider(); |
29 | 26 |
|
30 | 27 | const args = process.argv.slice(2); |
31 | | -if (args.length !== 3) { |
| 28 | +if (args.length !== 5) { |
32 | 29 | console.error( |
33 | | - 'Unexpected number of arguments received. Consult the script header comments for expected arguments' |
| 30 | + "Unexpected number of arguments received. Consult the script header comments for expected arguments" |
34 | 31 | ); |
35 | 32 | process.exit(-1); |
36 | 33 | } |
37 | 34 |
|
38 | 35 | const filePath = args[0]; |
39 | 36 | const bucketName = args[1]; |
40 | 37 | const namespaceName = args[2]; |
41 | | -const serviceName = 'objectstorage' |
| 38 | +const regionId = args[3]; |
| 39 | +const secondLevelDomain = args[4]; |
| 40 | +const serviceName = "objectstorage"; |
42 | 41 |
|
43 | 42 | const client = new os.ObjectStorageClient({ |
44 | | - authenticationDetailsProvider: provider |
| 43 | + authenticationDetailsProvider: provider |
45 | 44 | }); |
46 | | -client.region = common.Region.US_PHOENIX_1; |
47 | 45 |
|
48 | 46 | (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); |
| 47 | + try { |
| 48 | + console.time(`Download Time ${filePath}`); |
56 | 49 |
|
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); |
| 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); |
75 | 54 |
|
76 | | - console.timeEnd(`Download Time ${filePath}`); |
77 | | - } catch (error) { |
78 | | - console.log('Error executing example ' + error); |
79 | | - } |
| 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: os.models.CreatePreauthenticatedRequestDetails.AccessType.ObjectRead, |
| 61 | + timeExpires: neverExpires |
| 62 | + }; |
| 63 | + const createPreauthenticatedRequest = { |
| 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}.${regionId}.${secondLevelDomain}`; |
| 71 | + const downloadUrl = resp.preauthenticatedRequest.accessUri; |
| 72 | + console.log("download url for the file " + filePath + " is " + baseUrl + downloadUrl); |
80 | 73 |
|
| 74 | + console.timeEnd(`Download Time ${filePath}`); |
| 75 | + } catch (error) { |
| 76 | + console.log("Error executing example " + error); |
| 77 | + } |
81 | 78 | })(); |
0 commit comments