|
| 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 | +})(); |
0 commit comments