gpt4 book ai didi

node.js - 尝试通过 Node js将zip文件上传到azure存储

转载 作者:行者123 更新时间:2023-12-03 01:25:06 24 4
gpt4 key购买 nike

尝试使用nodejs将文件上传到azure存储blob,但面临 promise 问题。下面是我的代码在将 zip 文件上传到 azure blob 存储时遇到的问题,需要将此代码转换为回调函数或 .then 函数

var re = /\.zip/;

fs.readdir("/tmp/", function(err, files) {
if (err) {
console.log( "Could not list the directory.", err)
process.exit( 1 )
}
console.log("files",files)

var matches = files.filter( function(text) { return re.test(text) } )
console.log("These are the files you have", matches)
var numFiles = matches.length
console.log(numFiles);

if ( numFiles ) {
// Read in the file, convert it to base64, store to S3

for(let i = 0; i < numFiles; i++ ) {

uploadFileToBlob(matches[i])
.then((result: any) => {
console.log('result');
console.log(result);
})
.catch((error: any) => {
console.log(error);
});
}
}
})
const uploadFileToBlob = async (file: any) => {
const { AbortController } = require("@azure/abort-controller");
const { AnonymousCredential, BlobServiceClient, newPipeline } = require("@azure/storage-blob");
const account = 'string';
const accountSas = "sastoken";
const localFilePath = file;
const pipeline = newPipeline(new AnonymousCredential(), {
// httpClient: MyHTTPClient, // A customized HTTP client implementing IHttpClient interface
retryOptions: { maxTries: 4 }, // Retry options
userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" }, // Customized telemetry string
keepAliveOptions: {
// Keep alive is enabled by default, disable keep alive by setting false
enable: false
}
});
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net${accountSas}`,
pipeline
);
const containerName = 'quickstart';
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobName = file;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
console.log("blockBlobClient",blockBlobClient);
try {
const result =await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev: any) => console.log(ev)
});
console.log("uploadFile succeeds");
return result;
} catch (err) {
console.log(
`uploadFile failed, requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
throw err;
}
}

获取输出

Promise { <pending> }

我尝试在回调函数或 .then 函数中编写代码,但出现相同的错误,并且相同的代码在本地计算机上运行

最佳答案

请更改以下代码行:

await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile succeeds");

const result = await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile succeeds");
return result;

然后调用您的 uploadFileToBlob 方法,如下所示:

uploadFileToBlob('filename.zip')
.then((result) => {
console.log('result');
console.log(result);
})
.catch((error) => {
console.log(error);
});

更新

这是我使用的完整代码。我能够成功上传文件:

const uploadFileToBlob = async (file) => {
const { AbortController } = require("@azure/abort-controller");
const { AnonymousCredential, BlobServiceClient, newPipeline } = require("@azure/storage-blob");
const account = 'account-name';
const accountSas = '?sv=2020-04-08&ss=b&srt=sco&se=2021-03-08T18%3A30%3A00Z&sp=rwdxftlacup&sig=pT1d1NJQdu3bcnPA37voTCyQ6jKnmNVYgo1wyiYspDc%3D';
const localFilePath = file;
const pipeline = newPipeline(new AnonymousCredential(), {
// httpClient: MyHTTPClient, // A customized HTTP client implementing IHttpClient interface
retryOptions: { maxTries: 4 }, // Retry options
userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" }, // Customized telemetry string
keepAliveOptions: {
// Keep alive is enabled by default, disable keep alive by setting false
enable: false
}
});
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net${accountSas}`,
pipeline
);
const containerName = 'container-name';
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobName = file;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
console.log("blockBlobClient",blockBlobClient);
try {
const result = await blockBlobClient.uploadFile(localFilePath, {
blockSize: 4 * 1024 * 1024, // 4MB block size
concurrency: 20, // 20 concurrency
onProgress: (ev) => console.log(ev)
});
console.log("uploadFile succeeds");
return result;
} catch (err) {
console.log(
`uploadFile failed, requestId - ${err.details.requestId}, statusCode - ${err.statusCode}, errorCode - ${err.details.errorCode}`
);
throw err;
}
}

uploadFileToBlob('filename.zip')
.then((result) => {
console.log('result');
console.log(result);
})
.catch((error) => {
console.log(error);
});

关于node.js - 尝试通过 Node js将zip文件上传到azure存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66530386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com