Làm cách nào để tải xuống tệp từ Azure BLOB Storage bằng JavaScript?

Tôi có thể tải tệp lên bộ lưu trữ Azure blob qua Node/Express mà không gặp sự cố, nhưng đã tìm thấy tài liệu rất tối thiểu/ví dụ đầy đủ về cách tải xuống tệp. Tôi tìm thấy cái này trên trang hướng dẫn, nhưng không gặp may

blobSvc.getBlobToStream('mycontainer', 'myblob', fs.createWriteStream('output.txt'), function(error, result, response){
  if(!error){
    // blob retrieved
  }
});

Có ai khác đã sử dụng nút. js/express để tải xuống các tệp từ bộ nhớ Azure blob? . Lời yêu cầu). Bạn có thể chia sẻ cách lấy tệp từ Azure và truyền tệp đó vào một thư mục không?

Giải pháp tốt nhất

Có hướng dẫn đầy đủ về cách sử dụng Azure Storage Blobs với Node. js ở đây. Hướng dẫn đề cập đến getBlobToLocalFile nghe giống như những gì bạn có thể đang tìm kiếm. Cú pháp của phương thức này phải cực kỳ giống với getBlobToStream nhưng thay vì một luồng, bạn sẽ cung cấp một đường dẫn tệp

Giải pháp liên quan

asp. net-mvc – Tải xuống tệp Azure Blob trong MVC3

Hai lựa chọn thực sự. cách đầu tiên là chỉ chuyển hướng người dùng trực tiếp đến đốm màu (nếu các đốm màu nằm trong vùng chứa công cộng). Điều đó sẽ trông hơi giống

return Redirect(container.GetBlobReference(name).Uri.AbsoluteUri);

Nếu đốm màu nằm trong vùng chứa riêng tư, bạn có thể sử dụng Chữ ký truy cập được chia sẻ và thực hiện chuyển hướng như ví dụ trước hoặc bạn có thể đọc đốm màu trong hành động của bộ điều khiển và đẩy nó xuống máy khách dưới dạng tải xuống

Response.AddHeader("Content-Disposition", "attachment; filename=" + name); // force download
container.GetBlobReference(name).DownloadToStream(Response.OutputStream);
return new EmptyResult();

Nút. js – Truyền phát tệp đã tải lên bộ nhớ Azure blob bằng Node

GIẢI PHÁP (dựa trên thảo luận với @danielepolencic)

Sử dụng Multiparty(npm install multiparty), một nhánh của Formidable, chúng tôi có thể truy cập dữ liệu nhiều phần nếu chúng tôi tắt phần mềm trung gian bodyparser() khỏi Express (xem ghi chú của họ về cách thực hiện việc này để biết thêm thông tin). Không giống như Formidable, Multiparty sẽ không truyền tệp vào đĩa trừ khi bạn yêu cầu nó

Azure Storage Blob là giải pháp lưu trữ đối tượng của Microsoft cho đám mây. Lưu trữ blob được tối ưu hóa để lưu trữ lượng lớn dữ liệu phi cấu trúc. Dữ liệu phi cấu trúc là dữ liệu không tuân theo một định nghĩa hoặc mô hình dữ liệu cụ thể, chẳng hạn như dữ liệu văn bản hoặc nhị phân

Dự án này cung cấp một thư viện máy khách bằng JavaScript giúp dễ dàng sử dụng dịch vụ Microsoft Azure Storage Blob

Sử dụng các thư viện máy khách trong gói này để

  • Nhận/Đặt thuộc tính dịch vụ Blob
  • Tạo/Liệt kê/Xóa vùng chứa
  • Tạo/Đọc/Liệt kê/Cập nhật/Xóa Block Blobs
  • Tạo/Đọc/Liệt kê/Cập nhật/Xóa Trang đốm màu
  • Tạo/Đọc/Liệt kê/Cập nhật/Xóa các đốm màu nối thêm

Mã nguồn. Gói (npm). Tài liệu tham khảo API. tài liệu sản phẩm. Mẫu. Azure Storage Blob REST API

Ý chính

Lưu trữ Blob được thiết kế cho

  • Cung cấp hình ảnh hoặc tài liệu trực tiếp tới trình duyệt
  • Lưu trữ tệp để truy cập phân tán
  • Truyền phát video và âm thanh
  • Ghi vào tệp nhật ký
  • Lưu trữ dữ liệu để sao lưu và khôi phục, khắc phục sự cố và lưu trữ
  • Lưu trữ dữ liệu để phân tích bằng dịch vụ tại chỗ hoặc được lưu trữ trên Azure

Lưu trữ blob cung cấp ba loại tài nguyên

  • Tài khoản lưu trữ được sử dụng qua BlobServiceClient
  • Một vùng chứa trong tài khoản lưu trữ được sử dụng qua
    const AzureStorageBlob = require("@azure/storage-blob");
    0
  • Một đốm màu trong vùng chứa được sử dụng qua
    const AzureStorageBlob = require("@azure/storage-blob");
    1

Bắt đầu

điều kiện tiên quyết. Bạn phải có đăng ký Azure và Tài khoản lưu trữ để sử dụng gói này. Nếu bạn đang sử dụng gói này trong một Nút. ứng dụng js, sau đó là Nút. js phiên bản 8. 0. 0 hoặc cao hơn là bắt buộc

Cách ưu tiên để cài đặt thư viện máy khách Azure Storage Blob cho JavaScript là sử dụng trình quản lý gói npm. Nhập nội dung sau vào cửa sổ đầu cuối

npm install @azure/storage-blob

Dịch vụ Azure Blob Storage hỗ trợ sử dụng Azure Active Directory để xác thực các yêu cầu đối với API của nó. Gói

const AzureStorageBlob = require("@azure/storage-blob");
2 cung cấp nhiều loại thông tin xác thực mà ứng dụng của bạn có thể sử dụng để thực hiện việc này. README cho
const AzureStorageBlob = require("@azure/storage-blob");
2 cung cấp thêm thông tin chi tiết và mẫu để giúp bạn bắt đầu

khả năng tương thích

Thư viện này tương thích với Node. js và trình duyệt, đồng thời được xác thực đối với LTS Node. phiên bản js (>=8. 16. 0) và các phiên bản mới nhất của Chrome, Firefox và Edge

Tương thích với IE11

Bạn cần có các polyfill để làm cho thư viện này hoạt động với IE11. Cách dễ nhất là sử dụng dịch vụ @babel/polyfill hoặc polyfill

Bạn cũng có thể tải các polyfill riêng biệt cho (các) tính năng ES bị thiếu. Thư viện này phụ thuộc vào các tính năng ES sau cần tải các polyfill bên ngoài

  • const AzureStorageBlob = require("@azure/storage-blob");
    4
  • const AzureStorageBlob = require("@azure/storage-blob");
    5
  • const AzureStorageBlob = require("@azure/storage-blob");
    6
  • const AzureStorageBlob = require("@azure/storage-blob");
    7
  • const AzureStorageBlob = require("@azure/storage-blob");
    8
  • const AzureStorageBlob = require("@azure/storage-blob");
    9
  • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
    0
  • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
    1 (Ghi đè
    const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
    1 của IE11 bằng polyfill ES6 để kích hoạt)
  • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
    3

Sự khác biệt giữa nút. js và trình duyệt

Có sự khác biệt giữa Nút. js và thời gian chạy trình duyệt. Khi bắt đầu với thư viện này, hãy chú ý đến các API hoặc lớp được đánh dấu "CHỈ CÓ SẴN TRONG NÚT. JS RUNTIME" hoặc "CHỈ CÓ TRONG TRÌNH DUYỆT"

Các tính năng, giao diện, lớp hoặc chức năng chỉ có trong Node. js
  • Ủy quyền khóa được chia sẻ dựa trên tên tài khoản và khóa tài khoản
    • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
      4
  • Tạo chữ ký truy cập chung (SAS)
    • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
      5
    • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
      6
  • Tải lên và tải xuống song song
    • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
      7
    • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
      8
    • const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
      9
    • const { DefaultAzureCredential } = require("@azure/identity");
      const { BlobServiceClient } = require("@azure/storage-blob");
      
      const account = "";
      const defaultAzureCredential = new DefaultAzureCredential();
      
      const blobServiceClient = new BlobServiceClient(
        `https://${account}.blob.core.windows.net`,
        defaultAzureCredential
      );
      
      async function main() {
        let i = 1;
        let iter = await blobServiceClient.listContainers();
        for await (const container of iter) {
          console.log(`Container ${i++}: ${container.name}`);
        }
      }
      
      main();
      0
Các tính năng, giao diện, lớp hoặc chức năng chỉ có trong trình duyệt
  • Tải lên và tải xuống song song
    • const { DefaultAzureCredential } = require("@azure/identity");
      const { BlobServiceClient } = require("@azure/storage-blob");
      
      const account = "";
      const defaultAzureCredential = new DefaultAzureCredential();
      
      const blobServiceClient = new BlobServiceClient(
        `https://${account}.blob.core.windows.net`,
        defaultAzureCredential
      );
      
      async function main() {
        let i = 1;
        let iter = await blobServiceClient.listContainers();
        for await (const container of iter) {
          console.log(`Container ${i++}: ${container.name}`);
        }
      }
      
      main();
      1

Gói JavaScript

Để sử dụng thư viện máy khách này trong trình duyệt, bạn cần sử dụng trình đóng gói. Để biết chi tiết về cách thực hiện việc này, vui lòng tham khảo tài liệu đi kèm của chúng tôi

CORS

Bạn cần thiết lập quy tắc Chia sẻ tài nguyên nguồn gốc chéo (CORS) cho tài khoản lưu trữ của mình nếu bạn cần phát triển cho các trình duyệt. Truy cập cổng Azure và Azure Storage Explorer, tìm tài khoản lưu trữ của bạn, tạo quy tắc CORS mới cho (các) dịch vụ blob/queue/tệp/bảng

Ví dụ: bạn có thể tạo các cài đặt CORS sau để gỡ lỗi. Nhưng vui lòng tùy chỉnh cài đặt cẩn thận theo yêu cầu của bạn trong môi trường sản xuất

  • nguồn gốc được phép. *
  • động từ được phép. XÓA, NHẬN, ĐẦU, HỢP NHẤT, ĐĂNG, TÙY CHỌN, ĐẶT
  • tiêu đề được phép. *
  • Tiêu đề tiếp xúc. *
  • Tuổi tối đa (giây). 86400

ví dụ

Nhập gói

Để sử dụng ứng dụng khách, hãy nhập gói vào tệp của bạn

const AzureStorageBlob = require("@azure/storage-blob");

Ngoài ra, chỉ nhập có chọn lọc những loại bạn cần

const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");

Tạo ứng dụng khách dịch vụ blob

Sử dụng hàm tạo để tạo một thể hiện của BlobServiceClient

  • Cách được đề xuất để khởi tạo một BlobServiceClient - với

    const { DefaultAzureCredential } = require("@azure/identity");
    const { BlobServiceClient } = require("@azure/storage-blob");
    
    const account = "";
    const defaultAzureCredential = new DefaultAzureCredential();
    
    const blobServiceClient = new BlobServiceClient(
      `https://${account}.blob.core.windows.net`,
      defaultAzureCredential
    );
    
    async function main() {
      let i = 1;
      let iter = await blobServiceClient.listContainers();
      for await (const container of iter) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
    
    main();
    4 từ gói
    const AzureStorageBlob = require("@azure/storage-blob");
    2

    Cài đặt. Tham khảo - Cho phép truy cập vào các đốm màu và hàng đợi với Azure Active Directory từ ứng dụng khách - https. // tài liệu. Microsoft. com/azure/storage/common/storage-auth-aad-app

    • Đăng ký ứng dụng AAD mới và cấp quyền truy cập Azure Storage thay mặt cho người dùng đã đăng nhập

      • Đăng ký ứng dụng mới trong Azure Active Directory(trong cổng thông tin Azure) - https. // tài liệu. Microsoft. com/azure/active-directory/develop/quickstart-register-app
      • Trong phần
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = await blobServiceClient.listContainers();
          for await (const container of iter) {
            console.log(`Container ${i++}: ${container.name}`);
          }
        }
        
        main();
        6, chọn
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = await blobServiceClient.listContainers();
          for await (const container of iter) {
            console.log(`Container ${i++}: ${container.name}`);
          }
        }
        
        main();
        7 và chọn
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = await blobServiceClient.listContainers();
          for await (const container of iter) {
            console.log(`Container ${i++}: ${container.name}`);
          }
        }
        
        main();
        8
      • Chọn
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = await blobServiceClient.listContainers();
          for await (const container of iter) {
            console.log(`Container ${i++}: ${container.name}`);
          }
        }
        
        main();
        9 và chọn hộp kiểm bên cạnh
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = blobServiceClient.listContainers();
          let containerItem = await iter.next();
          while (!containerItem.done) {
            console.log(`Container ${i++}: ${containerItem.value.name}`);
            containerItem = await iter.next();
          }
        }
        
        main();
        0 rồi nhấp vào
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = blobServiceClient.listContainers();
          let containerItem = await iter.next();
          while (!containerItem.done) {
            console.log(`Container ${i++}: ${containerItem.value.name}`);
            containerItem = await iter.next();
          }
        }
        
        main();
        1. Điều này sẽ cho phép ứng dụng truy cập Azure Storage thay mặt cho người dùng đã đăng nhập
    • Cấp quyền truy cập vào dữ liệu Azure Blob bằng RBAC trong Cổng thông tin Azure

    • Thiết lập môi trường cho mẫu

      • Từ trang tổng quan của Đơn đăng ký AAD của bạn, hãy ghi lại
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = blobServiceClient.listContainers();
          let containerItem = await iter.next();
          while (!containerItem.done) {
            console.log(`Container ${i++}: ${containerItem.value.name}`);
            containerItem = await iter.next();
          }
        }
        
        main();
        2 và
        const { DefaultAzureCredential } = require("@azure/identity");
        const { BlobServiceClient } = require("@azure/storage-blob");
        
        const account = "";
        const defaultAzureCredential = new DefaultAzureCredential();
        
        const blobServiceClient = new BlobServiceClient(
          `https://${account}.blob.core.windows.net`,
          defaultAzureCredential
        );
        
        async function main() {
          let i = 1;
          let iter = blobServiceClient.listContainers();
          let containerItem = await iter.next();
          while (!containerItem.done) {
            console.log(`Container ${i++}: ${containerItem.value.name}`);
            containerItem = await iter.next();
          }
        }
        
        main();
        3. Trong tab "Chứng chỉ & Bí mật", hãy tạo một bí mật và ghi lại điều đó
      • Đảm bảo rằng bạn có AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET làm biến môi trường để thực hiện thành công mẫu (Có thể tận dụng quy trình. vi)

    Xem mẫu Azure AD Auth để biết ví dụ hoàn chỉnh về cách sử dụng phương pháp này

    [Lưu ý - Các bước trên chỉ dành cho Node. js]

  • Ngoài ra, bạn khởi tạo một BlobServiceClient với một

    const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
    4 bằng cách chuyển tên tài khoản và khóa tài khoản làm đối số. (tên tài khoản và khóa tài khoản có thể được lấy từ cổng Azure) [CHỈ CÓ SẴN TRONG NÚT. THỜI GIAN CHẠY JS]

Tạo vùng chứa mới

Sử dụng

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  let iter = blobServiceClient.listContainers();
  let containerItem = await iter.next();
  while (!containerItem.done) {
    console.log(`Container ${i++}: ${containerItem.value.name}`);
    containerItem = await iter.next();
  }
}

main();
6 để lấy phiên bản máy khách vùng chứa, sau đó tạo tài nguyên vùng chứa mới

Liệt kê các container

Sử dụng hàm

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  let iter = blobServiceClient.listContainers();
  let containerItem = await iter.next();
  while (!containerItem.done) {
    console.log(`Container ${i++}: ${containerItem.value.name}`);
    containerItem = await iter.next();
  }
}

main();
7 để lặp lại các vùng chứa, với cú pháp
const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  let iter = blobServiceClient.listContainers();
  let containerItem = await iter.next();
  while (!containerItem.done) {
    console.log(`Container ${i++}: ${containerItem.value.name}`);
    containerItem = await iter.next();
  }
}

main();
8 mới

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  let iter = await blobServiceClient.listContainers();
  for await (const container of iter) {
    console.log(`Container ${i++}: ${container.name}`);
  }
}

main();

Ngoài ra, không cần sử dụng

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  let iter = blobServiceClient.listContainers();
  let containerItem = await iter.next();
  while (!containerItem.done) {
    console.log(`Container ${i++}: ${containerItem.value.name}`);
    containerItem = await iter.next();
  }
}

main();
8

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  let iter = blobServiceClient.listContainers();
  let containerItem = await iter.next();
  while (!containerItem.done) {
    console.log(`Container ${i++}: ${containerItem.value.name}`);
    containerItem = await iter.next();
  }
}

main();

Ngoài ra, phân trang cũng được hỗ trợ cho danh sách thông qua

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
  }
}

main();
0

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
  }
}

main();

Để biết mẫu hoàn chỉnh về bộ chứa lặp, vui lòng xem mẫu/bộ lặp-bộ chứa. ts

Tạo một đốm màu bằng cách tải dữ liệu lên

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

const containerName = "";

async function main() {
  const containerClient = blobServiceClient.getContainerClient(containerName);

  const content = "Hello world!";
  const blobName = "newblob" + new Date().getTime();
  const blockBlobClient = containerClient.getBlockBlobClient(blobName);
  const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
  console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
}

main();

Liệt kê các đốm màu bên trong một vùng chứa

Tương tự như danh sách container

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

const containerName = "";

async function main() {
  const containerClient = blobServiceClient.getContainerClient(containerName);

  let i = 1;
  let iter = await containerClient.listBlobsFlat();
  for await (const blob of iter) {
    console.log(`Blob ${i++}: ${blob.name}`);
  }
}

main();

Để có một mẫu hoàn chỉnh về các đốm màu lặp lại, vui lòng xem các mẫu/iterators-blobs. ts

Tải xuống một đốm màu và chuyển đổi nó thành một chuỗi (Node. js)

Tải xuống một đốm màu và chuyển đổi nó thành một chuỗi (Trình duyệt)

Vui lòng tham khảo phần để biết thêm thông tin về cách sử dụng thư viện này trong trình duyệt

Một ví dụ đầy đủ về các kịch bản cơ bản là tại mẫu/cơ bản. ts

Xử lý sự cố

Cho phép ghi nhật ký có thể giúp khám phá thông tin hữu ích về lỗi. Để xem nhật ký các yêu cầu và phản hồi HTTP, hãy đặt biến môi trường

const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
  }
}

main();
1 thành
const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
  }
}

main();
2. Ngoài ra, ghi nhật ký có thể được kích hoạt trong thời gian chạy bằng cách gọi
const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
  }
}

main();
3 trong
const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");

const account = "";
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  defaultAzureCredential
);

async function main() {
  let i = 1;
  for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
  }
}

main();
4

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

Bước tiếp theo

Thêm mẫu mã

Đóng góp

Dự án này hoan nghênh những đóng góp và đề xuất. Hầu hết các đóng góp đều yêu cầu bạn đồng ý với Thỏa thuận cấp phép cộng tác viên (CLA) tuyên bố rằng bạn có quyền và thực sự làm như vậy, cấp cho chúng tôi quyền sử dụng đóng góp của bạn. Để biết chi tiết, hãy truy cập https. //cla. Microsoft. com

Khi bạn gửi yêu cầu kéo, CLA-bot sẽ tự động xác định xem bạn có cần cung cấp CLA và trang trí PR một cách thích hợp hay không (e. g. , nhãn, nhận xét). Chỉ cần làm theo các hướng dẫn được cung cấp bởi bot. Bạn sẽ chỉ cần thực hiện việc này một lần trên tất cả các kho sử dụng CLA của chúng tôi

Dự án này đã áp dụng Bộ quy tắc ứng xử mã nguồn mở của Microsoft. Để biết thêm thông tin, hãy xem Câu hỏi thường gặp về Quy tắc Ứng xử hoặc liên hệ với opencode@microsoft. com với bất kỳ câu hỏi hoặc ý kiến ​​​​thêm

Làm cách nào để đọc dữ liệu Azure blob trong JavaScript?

Ví dụ .
Nhập gói
Tạo ứng dụng khách dịch vụ blob
Tạo vùng chứa mới
Liệt kê các container
Tạo blob bằng cách tải lên dữ liệu
Liệt kê các đốm màu bên trong một vùng chứa
Tải xuống một đốm màu và chuyển đổi nó thành một chuỗi (Node. js)
Tải xuống một đốm màu và chuyển đổi nó thành một chuỗi (Trình duyệt)

Làm cách nào để tải tệp lên bộ lưu trữ blob Azure bằng JavaScript?

Thiết lập môi trường phát triển. .
Ngã ba và sao chép ứng dụng mẫu. .
Cài đặt các phụ thuộc và chạy dự án cục bộ. .
Tạo tài nguyên lưu trữ với tiện ích mở rộng Visual Studio. .
Tạo mã thông báo chữ ký truy cập được chia sẻ (SAS) của bạn. .
Đặt giá trị lưu trữ trong. .
Định cấu hình CORS cho tài nguyên Lưu trữ Azure

JavaScript lưu trữ blob là gì?

Bộ lưu trữ Blob được tối ưu hóa để lưu trữ một lượng lớn dữ liệu phi cấu trúc . Dữ liệu phi cấu trúc là dữ liệu không tuân theo một định nghĩa hoặc mô hình dữ liệu cụ thể, chẳng hạn như dữ liệu văn bản hoặc nhị phân. Dự án này cung cấp một thư viện máy khách bằng JavaScript giúp dễ dàng sử dụng dịch vụ Microsoft Azure Blob Storage.