尝试通过节点 js 将 zip 文件上传到 azure 存储

Posted

技术标签:

【中文标题】尝试通过节点 js 将 zip 文件上传到 azure 存储【英文标题】:Trying to upload zip file to azure storage via node js 【发布时间】:2021-06-06 08:49:36 【问题描述】:

尝试使用 nodejs 将文件上传到天蓝色存储 blob,但面临承诺问题。下面是我在将 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 函数中编写代码,但得到相同的错误,并且相同的代码在本地机器上运行

【问题讨论】:

你从哪里得到这个Promise &lt;pending&gt; 输出? 里面console.log("uploadFile succeeds",blockBlobClient.uploadFile(localFilePath, blockSize: 4 * 1024 * 1024, // 4MB 块大小 concurrency: 20, // 20 concurrency onProgress: (ev : any) => console.log(ev) )); 【参考方案1】:

请更改以下代码行:

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);
);

【讨论】:

您是否删除了从流中上传的代码?你真的不需要那个。 用我使用的完整代码更新了我的答案。 我更新了问题,你能用你的方法检查一下,但没有给出同样的错误 我试过你的更新代码,它对我来说很好。 从本地它正在工作,但在部署到 aws lambda serverless 之后,它给出了 Promise

以上是关于尝试通过节点 js 将 zip 文件上传到 azure 存储的主要内容,如果未能解决你的问题,请参考以下文章

从 s3 存储桶获取 2 个文件,并在使用 lambda 节点 js 将其上传到 s3 存储桶后制作 1 个 zip 文件

如何将zip文件上传到ruby中的github版本

Grails 3 Web界面,通过表单将zip上传到服务器上的文件夹

无法通过metaservlet api将talend作业zip文件从Windows机器上传到tac

如何通过 CLI 将 zip 文件上传和部署到 AWS elastic beanstalk?

Boto3没有将zip文件上传到S3 python