Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions oss/source/custom-code/ossClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FileTransferConfigurations } from "./fileTransferConfigurations";
import { promises as fs } from "fs";
import { Stream } from "stream";


export class OssClient extends BaseClient {

public objectApi: ObjectsApi;
Expand All @@ -23,6 +24,7 @@ export class OssClient extends BaseClient {
this.ossFileTransfer = new OSSFileTransfer(new FileTransferConfigurations(3), optionalArgs.sdkManager, optionalArgs.authenticationProvider);

}

/**
*Downloads a file by transparently handling operations like obtaining signed download URLs and chunking large files for optimal transfer.
* @param {string} bucketKey URL-encoded bucket key
Expand All @@ -35,6 +37,9 @@ export class OssClient extends BaseClient {
* @throws {RequiredError}
* @memberof OSSApiInterface
*/
/**
* @deprecated Use the `downloadObject` method instead.
*/
public async downloadObjectAsync(bucketKey: string, objectKey: string, filePath: string, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<void>;
/**
*Downloads a file by transparently handling operations like obtaining signed download URLs and chunking large files for optimal transfer.
Expand All @@ -49,7 +54,13 @@ export class OssClient extends BaseClient {
* @memberof OSSApiInterface
*
*/
/**
* @deprecated Use the `downloadObject` method instead.
*/
public async downloadObjectAsync(bucketKey: string, objectKey: string, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<Stream>;
/**
* @deprecated Use the `downloadObject` method instead.
*/
public async downloadObjectAsync(bucketKey: string, objectKey: string, filePathOrOptionalArgs: string | { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<void | Stream> {
let filePath: string | undefined;
if (typeof filePathOrOptionalArgs === 'string') {
Expand All @@ -71,6 +82,55 @@ export class OssClient extends BaseClient {
}
}

/**
*Downloads a file by transparently handling operations like obtaining signed download URLs and chunking large files for optimal transfer.
* @param {string} bucketKey URL-encoded bucket key
* @param {string} objectKey URL-encoded object name
* @param {string} filePath The Path of the file where should be downloaded
* @param accessToken bearer access token
* @param {AbortController} cancellationToken
* @param {string} requestIdPrefix
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof OSSApiInterface
*/
public async downloadObject(bucketKey: string, objectKey: string, filePath: string, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<void>;
/**
*Downloads a file by transparently handling operations like obtaining signed download URLs and chunking large files for optimal transfer.
* @param {string} bucketKey URL-encoded bucket key
* @param {string} objectKey URL-encoded object name
* @param accessToken bearer access token
* @param {AbortController} cancellationToken
* @param {string} requestIdPrefix
* @param {*} [options] Override http request option.
* @returns {Stream}
* @throws {RequiredError}
* @memberof OSSApiInterface
*
*/
public async downloadObject(bucketKey: string, objectKey: string, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<Stream>;
public async downloadObject(bucketKey: string, objectKey: string, filePathOrOptionalArgs: string | { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<void | Stream> {
let filePath: string | undefined;
if (typeof filePathOrOptionalArgs === 'string') {
filePath = filePathOrOptionalArgs;
} else {
optionalArgs = filePathOrOptionalArgs;
}

if (!optionalArgs?.accessToken && !this.authenticationProvider) {
throw new Error("Please provide a valid access token or an authentication provider");
} else if (!optionalArgs?.accessToken) {
(optionalArgs ??= {}).accessToken = await this.authenticationProvider.getAccessToken();
}

if (filePath) {
await this.ossFileTransfer.download(bucketKey, objectKey, optionalArgs?.accessToken, filePath, optionalArgs?.cancellationToken || new AbortController(), optionalArgs?.requestIdPrefix, optionalArgs?.onProgress);
} else {
return this.ossFileTransfer.download(bucketKey, objectKey, optionalArgs?.accessToken, null, optionalArgs?.cancellationToken || new AbortController(), optionalArgs?.requestIdPrefix, optionalArgs?.onProgress);
}
}


/**
* Instructs OSS to complete the object creation process for numerous objects after their bytes have been uploaded directly to S3. An object will not be accessible until you complete the object creation process, either with this endpoint or the single Complete Upload endpoint. This endpoint accepts batch sizes of up to 25. Any larger and the request will fail.
* @param {string} bucketKey URL-encoded bucket key
Expand All @@ -83,6 +143,9 @@ export class OssClient extends BaseClient {
* @throws {RequiredError}
* @memberof OSSApiInterface
*/
/**
* @deprecated Use the `uploadObject` method instead.
*/
public async uploadObjectAsync(bucketKey: string, objectKey: string, sourceToUpload: Buffer | string, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<ObjectDetails> {
if (!optionalArgs?.accessToken && !this.authenticationProvider) {
throw new Error("Please provide a valid access token or an authentication provider");
Expand All @@ -100,6 +163,39 @@ export class OssClient extends BaseClient {
}
return response.content;
}


/**
* Instructs OSS to complete the object creation process for numerous objects after their bytes have been uploaded directly to S3. An object will not be accessible until you complete the object creation process, either with this endpoint or the single Complete Upload endpoint. This endpoint accepts batch sizes of up to 25. Any larger and the request will fail.
* @param {string} bucketKey URL-encoded bucket key
* @param {string} objectKey URL-encoded object name
* @param {Buffer|string} sourceToUpload The Path of the file to be uploaded or the buffer of the file .
* @param accessToken bearer access token
* @param {AbortController} cancellationToken
* @param {string} requestIdPrefix
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof OSSApiInterface
*/
public async uploadObject(bucketKey: string, objectKey: string, sourceToUpload: Buffer | string, optionalArgs?: { cancellationToken?: AbortController, requestIdPrefix?: string, accessToken?: string, onProgress?: (percentCompleted: number) => void }): Promise<ObjectDetails> {
if (!optionalArgs?.accessToken && !this.authenticationProvider) {
throw new Error("Please provide a valid access token or an authentication provider");
}
else if (!optionalArgs?.accessToken) {
(optionalArgs ??= {}).accessToken = await this.authenticationProvider.getAccessToken();
}
var response;
if (typeof sourceToUpload === 'string') {
var buffer = await fs.readFile(sourceToUpload);
response = await this.ossFileTransfer.upload(bucketKey, objectKey, buffer, optionalArgs?.accessToken, optionalArgs?.cancellationToken || new AbortController, optionalArgs?.requestIdPrefix, optionalArgs?.onProgress);
}
else {
response = await this.ossFileTransfer.upload(bucketKey, objectKey, sourceToUpload, optionalArgs?.accessToken, optionalArgs?.cancellationToken || new AbortController, optionalArgs?.requestIdPrefix, optionalArgs?.onProgress);
}
return response.content;
}


/**
* Instructs OSS to complete the object creation process for numerous objects after their bytes have been uploaded directly to S3. An object will not be accessible until you complete the object creation process, either with this endpoint or the single Complete Upload endpoint. This endpoint accepts batch sizes of up to 25. Any larger and the request will fail.
* @param {string} bucketKey URL-encoded bucket key
Expand Down
1 change: 1 addition & 0 deletions oss/source/custom-code/ossFileTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class OSSFileTransfer implements IOSSFileTransfer {
let partsDownloaded: number = 0;
let start: number = 0;
let outStream = new PassThrough();
outStream.setMaxListeners(numberOfChunks+1);
let fileStream: WriteStream;
if(filePath){
fileStream= createWriteStream(filePath, { flags: 'a' });
Expand Down
2 changes: 1 addition & 1 deletion oss/source/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aps_sdk/oss",
"version": "1.0.0",
"version": "1.0.1",
"description": "Client sdk for Data Management Oss API",
"license": "Apache-2.0",
"author": "Autodesk Platform Services",
Expand Down
6 changes: 3 additions & 3 deletions samples/oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ const ossClient = new OssClient({ authenticationProvider: staticAuthenticationPr
async function upload() {

//sourceToUpload can be either file path or stream of the object
const response = await ossClient.uploadObjectAsync(bucketKey, objectKey, sourceToUpload);
const response = await ossClient.uploadObject(bucketKey, objectKey, sourceToUpload);
console.log(response);
}
//Downloaing a file can be directly to the file or get the stream of file.
async function download() {
await ossClient.downloadObjectAsync(bucketKey, objectKey, filePath);
await ossClient.downloadObject(bucketKey, objectKey, filePath);
}

async function downloadAsStream() {
let fileStream = new Stream();
fileStream = await ossClient.downloadObjectAsync(bucketKey, objectKey);
fileStream = await ossClient.downloadObject(bucketKey, objectKey);
}
/**
* This function will return the details about the specified bucket.
Expand Down
Loading