Skip to content

Commit 18c6d38

Browse files
committed
mad namespace and filename as a commandline argument
1 parent a2d1a12 commit 18c6d38

File tree

2 files changed

+60
-84
lines changed

2 files changed

+60
-84
lines changed

examples/javascript/create-preauth-req.js

+28-40
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ const provider = new common.ConfigFileAuthenticationDetailsProvider(
2828
);
2929

3030
const args = process.argv.slice(2);
31-
if (args.length !== 2) {
31+
if (args.length !== 3) {
3232
console.error(
3333
'Unexpected number of arguments received. Consult the script header comments for expected arguments'
3434
);
3535
process.exit(-1);
3636
}
3737

38-
const directoryPath = args[0];
38+
const filePath = args[0];
3939
const bucketName = args[1];
40+
const namespaceName = args[2];
4041
const serviceName = 'objectstorage'
4142

4243
const client = new os.ObjectStorageClient({
@@ -46,48 +47,35 @@ client.region = common.Region.US_PHOENIX_1;
4647

4748
(async () => {
4849
try {
49-
console.log('Getting the namespace...');
50-
const request = {};
51-
const response = await client.getNamespace(request);
52-
const namespaceName = response.value;
53-
54-
// Read files from the directory
55-
readdir(directoryPath, (err, files) => {
56-
if (err) return console.log('Unable to scan directory: ' + err);
50+
console.time(`Download Time ${filePath}`);
5751

58-
files.forEach(async filename => {
59-
// creating pre authentication request which generates the download url for the file
60-
console.time(`Download Time ${filename}`);
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);
6156

62-
// set expiry date for the download url.
63-
const today = new Date();
64-
const neverExpires = new Date(today);
65-
neverExpires.setDate(neverExpires.getDate() + 25);
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);
6675

67-
// use date for generating a random unique id which can be used as a par id
68-
const parUniqueId = Date.now();
69-
const createPreauthenticatedRequestDetails = {
70-
name: parUniqueId.toString(),
71-
objectName: filename,
72-
accessType: os.models.CreatePreauthenticatedRequestDetails.AccessType.ObjectRead,
73-
timeExpires: neverExpires
74-
};
75-
const createPreauthenticatedRequest = {
76-
bucketName: bucketName,
77-
namespaceName: namespaceName,
78-
createPreauthenticatedRequestDetails: createPreauthenticatedRequestDetails
79-
};
80-
// create pre authenticated request to generate the url
81-
const resp = await client.createPreauthenticatedRequest(createPreauthenticatedRequest);
82-
const baseUrl = `https://${serviceName}.${common.Region.US_PHOENIX_1.regionId}.${common.Realm.OC1.secondLevelDomain}`
83-
const downloadUrl = resp.preauthenticatedRequest.accessUri;
84-
console.log('download url for the file ' + filename + ' is ' + baseUrl + downloadUrl);
85-
86-
console.timeEnd(`Download Time ${filename}`);
87-
});
88-
});
76+
console.timeEnd(`Download Time ${filePath}`);
8977
} catch (error) {
9078
console.log('Error executing example ' + error);
91-
}
79+
}
9280

9381
})();

examples/typescript/create-preauth-req.ts

+32-44
Original file line numberDiff line numberDiff line change
@@ -27,64 +27,52 @@ const provider: common.ConfigFileAuthenticationDetailsProvider = new common.Conf
2727
*/
2828

2929
const args = process.argv.slice(2);
30-
if (args.length !== 2) {
30+
if (args.length !== 3) {
3131
console.error(
3232
"Unexpected number of arguments received. Please pass absloute directory path to read files from"
3333
);
3434
process.exit(-1);
3535
}
3636

37-
const directoryPath: string = args[0]; // for eg : "/Users/Abc/upload-manager";
37+
const filePath: string = args[0]; // for eg : "/Users/Abc/upload-manager";
3838
const bucketName = args[1];
39+
const namespaceName = args[2]
3940
const serviceName = "objectstorage"
4041

4142
const client = new ObjectStorageClient({ authenticationDetailsProvider: provider });
4243
client.region = Region.US_PHOENIX_1;
4344

4445
(async () => {
45-
// getting the namespsace
46-
console.log("Getting the namespace...");
47-
const request: requests.GetNamespaceRequest = {};
48-
const response = await client.getNamespace(request);
49-
const namespaceName = response.value;
46+
try {
47+
// creating pre authentication request which generates the download url for the file
48+
console.time(`Download Time ${filePath}`);
5049

51-
// Read files from the directory
52-
readdir(directoryPath, (err, files) => {
53-
if (err) return console.log("Unable to scan directory: " + err);
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);
5454

55-
files.forEach(async filename => {
56-
try {
57-
// creating pre authentication request which generates the download url for the file
58-
console.time(`Download Time ${filename}`);
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);
5973

60-
// set expiry date for the download url.
61-
const today = new Date();
62-
const neverExpires = new Date(today);
63-
neverExpires.setDate(neverExpires.getDate() + 25);
64-
65-
// use date for generating a random unique id which can be used as a par id
66-
const parUniqueId = Date.now();
67-
const createPreauthenticatedRequestDetails = {
68-
name: parUniqueId.toString(),
69-
objectName: filename,
70-
accessType: models.CreatePreauthenticatedRequestDetails.AccessType.ObjectRead,
71-
timeExpires: neverExpires
72-
} as models.CreatePreauthenticatedRequestDetails;
73-
const createPreauthenticatedRequest: requests.CreatePreauthenticatedRequestRequest = {
74-
bucketName: bucketName,
75-
namespaceName: namespaceName,
76-
createPreauthenticatedRequestDetails: createPreauthenticatedRequestDetails
77-
};
78-
// create pre authenticated request to generate the url
79-
const resp = await client.createPreauthenticatedRequest(createPreauthenticatedRequest);
80-
const baseUrl = `https://${serviceName}.${common.Region.US_PHOENIX_1.regionId}.${common.Realm.OC1.secondLevelDomain}`
81-
const downloadUrl = resp.preauthenticatedRequest.accessUri;
82-
console.log("download url for the file " + filename + " is " + baseUrl + downloadUrl);
83-
84-
console.timeEnd(`Download Time ${filename}`);
85-
} catch (ex) {
86-
console.error(`Failed due to ${ex}`);
87-
}
88-
});
89-
});
74+
console.timeEnd(`Download Time ${filePath}`);
75+
} catch (ex) {
76+
console.error(`Failed due to ${ex}`);
77+
}
9078
})();

0 commit comments

Comments
 (0)