Skip to content

Commit

Permalink
Merge pull request #66 from AlexanderYW/v5-feature/implement-put-apis
Browse files Browse the repository at this point in the history
Implemented put api's
  • Loading branch information
AlexanderYW authored Oct 10, 2021
2 parents 8674f05 + 0ae0a86 commit b7b8510
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/Drivers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@ioc:Adonis/Core/Drive'

import { promisify } from 'util'
import { pipeline, Readable } from 'stream'
import { PassThrough, pipeline } from 'stream'
import { string } from '@poppinss/utils/build/helpers'

import { DefaultAzureCredential } from '@azure/identity'
Expand All @@ -35,6 +35,8 @@ import {
BlobSASPermissions,
generateBlobSASQueryParameters,
BlobSASSignatureValues,
BlockBlobUploadOptions,
BlockBlobUploadStreamOptions,
} from '@azure/storage-blob'

/*
Expand Down Expand Up @@ -216,4 +218,34 @@ export class AzureStorageDriver implements AzureStorageDriverContract {
public async getUrl(location: string): Promise<string> {
return unescape(this.getBlockBlobClient(location).url)
}

/**
* Write string|buffer contents to a destination. The missing
* intermediate directories will be created (if required).
*/
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
} catch (error) {
throw CannotWriteFileException.invoke(location, error)
}
}

/**
* Write a stream to a destination. The missing intermediate
* directories will be created (if required).
*/
public async putStream(location: string, contents: NodeJS.ReadableStream, options?: BlockBlobUploadStreamOptions): Promise<void> {
const blockBlobClient = this.getBlockBlobClient(location)

try {
const stream = new PassThrough()
stream.write(contents)
await blockBlobClient.uploadStream(stream, undefined, undefined, options)

} catch (error) {
throw CannotWriteFileException.invoke(location, error)
}
}
}

0 comments on commit b7b8510

Please sign in to comment.