Skip to content

Commit

Permalink
Merge pull request #70 from AlexanderYW/v5-feature/implement-copy
Browse files Browse the repository at this point in the history
Implemented copy api
  • Loading branch information
AlexanderYW committed Oct 12, 2021
2 parents b7b8510 + c6a4a68 commit f14b997
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Drivers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export class AzureStorageDriver implements AzureStorageDriverContract {
public async generateBlobSASURL(blockBlobClient, options: BlobSASSignatureValues): Promise<string> {
options.permissions = BlobSASPermissions.parse(options.permissions as unknown as string || 'r')

options.expiresOn = options.expiresOn || new Date(options.startsOn.valueOf() + 3600 * 1000)
options.startsOn = options.startsOn || new Date()
options.expiresOn = options.expiresOn || new Date(options.startsOn.valueOf() + 3600 * 1000)

const blobSAS = await generateBlobSASQueryParameters(
{
Expand Down Expand Up @@ -222,11 +222,13 @@ export class AzureStorageDriver implements AzureStorageDriverContract {
/**
* Write string|buffer contents to a destination. The missing
* intermediate directories will be created (if required).
*
* @todo look into returning the response of upload
*/
public async put(location: string, contents: Buffer | string, options: BlockBlobUploadOptions | undefined): Promise<void> {
const blockBlobClient = this.getBlockBlobClient(location)
try {
return await blockBlobClient.upload(contents, contents.length, options) as unknown as void
await blockBlobClient.upload(contents, contents.length, options)
} catch (error) {
throw CannotWriteFileException.invoke(location, error)
}
Expand All @@ -248,4 +250,23 @@ export class AzureStorageDriver implements AzureStorageDriverContract {
throw CannotWriteFileException.invoke(location, error)
}
}

/**
* Copy a given location path from the source to the desination.
* The missing intermediate directories will be created (if required)
*
* @todo look into returning the response of syncCopyFromURL
*/
public async copy(source: string, destination: string, options: BlobSASSignatureValues): Promise<void> {
const sourceBlockBlobClient = this.getBlockBlobClient(source)
const destinationBlockBlobClient = this.getBlockBlobClient(destination)

const url = await this.generateBlobSASURL(sourceBlockBlobClient, options)

try {
await destinationBlockBlobClient.syncCopyFromURL(url)
} catch (error) {
throw CannotCopyFileException.invoke(source, destination, error.original || error)
}
}
}

0 comments on commit f14b997

Please sign in to comment.