azure 如何在BLOB中实现cwd和其他方法?

whhtz7ly  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(109)

我有一个抽象类,我已经扩展了SFTP类型。我还想用AZURE BLOB类型功能扩展这个类。

export abstract class IDataSource {
  constructor(public logger: Logger, public debug: boolean) { }

  abstract list(path: string): Promise<File[]>;
  abstract read(path: string, writeStream: NodeJS.WritableStream): Promise<void>;
  abstract write(path: string, readStream: NodeJS.ReadableStream): Promise<void>;
  abstract createReadStream(path: string): Promise<Readable>;
  abstract createWriteStream(path: string): Promise<Writable>;
  abstract close(): Promise<void>;
  abstract exists(path: string, isFile: boolean): Promise<boolean>;
  abstract delete(path: string): Promise<void>;
  abstract removeDir(path: string): Promise<void>;
  abstract makeDir(path: string, recursive: boolean): Promise<void>;
  abstract cwd(): Promise<string>; 
 }

如何使用抽象方法实现blob存储相关功能。

p4rjhz4m

p4rjhz4m1#

我引用了MSDOCCreateWriteStream来上传给定的内容文本到blob。

const  fs = require('fs');
const  path = require('path');
const { BlobServiceClient } = require('@azure/storage-blob');
const  connectionString = 'connection String';
const  containerName = 'sampath78';
const  blobName = 'file.txt';
const  filePath = path.join(__dirname, 'file.txt');
async  function  uploadToAzureBlobStorage() {
console.log('Writing data to local file...');
const  writeStream = fs.createWriteStream(filePath);
writeStream.write('Hello world!');
writeStream.end();
console.log('Uploading file to Azure Blob storage...');
const  blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const  containerClient = blobServiceClient.getContainerClient(containerName);
const  blockBlobClient = containerClient.getBlockBlobClient(blobName);
const  stream = fs.createReadStream(filePath);
await  blockBlobClient.uploadStream(stream);
console.log(`File ${blobName} uploaded to Azure Blob storage.`);
}
uploadToAzureBlobStorage();

使用createReadStream Output

Azure:

其他方法:

async  function  uploadBlob(containerName, blobName, data) {
const  blobServiceClient = BlobServiceClient.fromConnectionString("connectionstring");
const  containerClient = blobServiceClient.getContainerClient(containerName);
const  blockBlobClient = containerClient.getBlockBlobClient(blobName);
const  stream = Readable.from(data);
const  uploadOptions = {
bufferSize:  4 * 1024 * 1024,
maxBuffers:  20,
};
const  uploadResponse = await  blockBlobClient.uploadStream(
stream,
uploadOptions.bufferSize,
uploadOptions.maxBuffers,
{ blobHTTPHeaders: { blobContentType:  "text/plain" } }
);
console.log(

`Upload block blob ${blobName} successfully`,
uploadResponse.requestId
);
}
// Example usage

const  containerName = "sampath78";

const  blobName = "Hello.txt";

const  data = "Hello, world! Hi ";

uploadBlob(containerName, blobName, data).catch((error) => {

console.error("Error uploading blob:", error);

});

输出:

Azure:

阅读bolb中的内容

async  function  readBlob(containerName: string, blobName: string) {
const  blobServiceClient = BlobServiceClient.fromConnectionString( "connectionstring" );
const  containerClient = blobServiceClient.getContainerClient(containerName);
const  blockBlobClient = containerClient.getBlockBlobClient(blobName);
const  blobData = await  blockBlobClient.downloadToBuffer();
console.log(`Content of blob ${blobName}: ${blobData.toString()}`);

}

const  containerName = "sampath78";
const  blobName = "Hello.txt";

  

readBlob(containerName, blobName).catch((error) => {

console.error("Error reading blob:", error);

});

输出:

相关问题