你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ContainerClient class

ContainerClient 表示 Azure 存储容器的 URL,允许你作其 Blob。

扩展

构造函数

ContainerClient(string, PipelineLike)

创建 ContainerClient 的实例。 此方法接受指向容器的 URL。 编码的 URL 字符串不会转义两次,只会转义 URL 路径中的特殊字符。 如果 Blob 名称包含? 或 %,blob 名称必须在 URL 中编码。

ContainerClient(string, StorageSharedKeyCredential | AnonymousCredential | TokenCredential, StoragePipelineOptions)

创建 ContainerClient 的实例。 此方法接受指向容器的 URL。 编码的 URL 字符串不会转义两次,只会转义 URL 路径中的特殊字符。 如果 Blob 名称包含? 或 %,blob 名称必须在 URL 中编码。

ContainerClient(string, string, StoragePipelineOptions)

创建 ContainerClient 的实例。

属性

accountName
containerName

容器的名称。

credential

例如 AnonymousCredential、StorageSharedKeyCredential 或任何来自 @azure/identity 包的凭据,用于对服务的请求进行身份验证。 还可以提供实现 TokenCredential 接口的对象。 如果未指定,则使用 AnonymousCredential。

url

编码的 URL 字符串值。

方法

create(ContainerCreateOptions)

在指定的帐户下创建新容器。 如果已存在同名的容器,作将失败。

createIfNotExists(ContainerCreateOptions)

在指定的帐户下创建新容器。 如果已存在同名的容器,则不会更改。

delete(ContainerDeleteMethodOptions)

标记要删除的指定容器。 容器及其中包含的任何 blob 稍后将在垃圾回收期间删除。

deleteBlob(string, ContainerDeleteBlobOptions)

标记要删除的指定 Blob 或快照。 稍后在垃圾回收期间删除该 Blob。 请注意,若要删除 Blob,必须删除其所有快照。 可以使用“删除 Blob”作同时删除两者。

deleteIfExists(ContainerDeleteMethodOptions)

标记指定的容器以供删除(如果存在)。 容器及其中包含的任何 blob 稍后将在垃圾回收期间删除。

exists(ContainerExistsOptions)

如果此客户端表示的 Azure 容器资源存在,则返回 true;否则为 false。 注意:请谨慎使用此函数,因为其他客户端或应用程序可能会删除现有容器。 反之亦然,此函数完成后,其他客户端或应用程序可能会添加具有相同名称的新容器。

generateSasUrl(ContainerGenerateSasUrlOptions)

仅适用于使用共享密钥凭据构造的 ContainerClient。 基于传入的客户端属性和参数生成 Blob 容器服务共享访问签名(SAS)URI。 SAS 由客户端的共享密钥凭据签名。

getAccessPolicy(ContainerGetAccessPolicyOptions)

获取指定容器的权限。 权限指示是否可以公开访问容器数据。 警告:分析 startOn 和 expiresOn 字符串时,JavaScript 日期可能会丢失精度。 例如,新日期(“2018-12-31T03:44:23.8827891Z”)将获取“2018-12-31T03:44:23.882Z”。

getAppendBlobClient(string)

创建 <xref:AppendBlobClient>

getBlobBatchClient()

创建 BlobBatchClient 对象以执行批处理作。

getBlobClient(string)

创建 <xref:BlobClient>

getBlobLeaseClient(string)

获取管理容器租约的 <xref:BlobLeaseClient>。

getBlockBlobClient(string)

创建 <xref:BlockBlobClient>

getPageBlobClient(string)

创建 <xref:PageBlobClient>

getProperties(ContainerGetPropertiesOptions)

返回指定容器的所有用户定义的元数据和系统属性。 返回的数据不包括容器的 Blob 列表。

listBlobsByHierarchy(string, ContainerListBlobsOptions)

返回异步可迭代迭代器以按层次结构列出所有 Blob。 在指定的帐户下。 .byPage() 返回异步可迭代迭代迭代器,以按页中的层次结构列出 Blob。

使用 for await 语法的示例:

for await (const item of containerClient.listBlobsByHierarchy("/")) {
  if (item.kind === "prefix") {
    console.log(`\tBlobPrefix: ${item.name}`);
  } else {
    console.log(`\tBlobItem: name - ${item.name}, last modified - ${item.properties.lastModified}`);
  }
}

使用 iter.next()的示例:

let iter = containerClient.listBlobsByHierarchy("/", { prefix: "prefix1/" });
let entity = await iter.next();
while (!entity.done) {
  let item = entity.value;
  if (item.kind === "prefix") {
    console.log(`\tBlobPrefix: ${item.name}`);
  } else {
    console.log(`\tBlobItem: name - ${item.name}, last modified - ${item.properties.lastModified}`);
  }
  entity = await iter.next();
}

使用 byPage()的示例:

console.log("Listing blobs by hierarchy by page");
for await (const response of containerClient.listBlobsByHierarchy("/").byPage()) {
  const segment = response.segment;
  if (segment.blobPrefixes) {
    for (const prefix of segment.blobPrefixes) {
      console.log(`\tBlobPrefix: ${prefix.name}`);
    }
  }
  for (const blob of response.segment.blobItems) {
    console.log(`\tBlobItem: name - ${blob.name}, last modified - ${blob.properties.lastModified}`);
  }
}

使用具有最大页面大小的分页的示例:

console.log("Listing blobs by hierarchy by page, specifying a prefix and a max page size");

let i = 1;
for await (const response of containerClient.listBlobsByHierarchy("/", { prefix: "prefix2/sub1/"}).byPage({ maxPageSize: 2 })) {
  console.log(`Page ${i++}`);
  const segment = response.segment;

  if (segment.blobPrefixes) {
    for (const prefix of segment.blobPrefixes) {
      console.log(`\tBlobPrefix: ${prefix.name}`);
    }
  }

  for (const blob of response.segment.blobItems) {
    console.log(`\tBlobItem: name - ${blob.name}, last modified - ${blob.properties.lastModified}`);
  }
}
listBlobsFlat(ContainerListBlobsOptions)

返回异步可迭代迭代器以列出指定帐户下的所有 Blob。 .byPage() 返回异步可迭代迭代器以列出页面中的 blob。

使用 for await 语法的示例:

// Get the containerClient before you run these snippets,
// Can be obtained from `blobServiceClient.getContainerClient("<your-container-name>");`
let i = 1;
for await (const blob of containerClient.listBlobsFlat()) {
  console.log(`Blob ${i++}: ${blob.name}`);
}

使用 iter.next()的示例:

let i = 1;
let iter = containerClient.listBlobsFlat();
let blobItem = await iter.next();
while (!blobItem.done) {
  console.log(`Blob ${i++}: ${blobItem.value.name}`);
  blobItem = await iter.next();
}

使用 byPage()的示例:

// passing optional maxPageSize in the page settings
let i = 1;
for await (const response of containerClient.listBlobsFlat().byPage({ maxPageSize: 20 })) {
  for (const blob of response.segment.blobItems) {
    console.log(`Blob ${i++}: ${blob.name}`);
  }
}

对标记使用分页的示例:

let i = 1;
let iterator = containerClient.listBlobsFlat().byPage({ maxPageSize: 2 });
let response = (await iterator.next()).value;

// Prints 2 blob names
for (const blob of response.segment.blobItems) {
  console.log(`Blob ${i++}: ${blob.name}`);
}

// Gets next marker
let marker = response.continuationToken;

// Passing next marker as continuationToken

iterator = containerClient.listBlobsFlat().byPage({ continuationToken: marker, maxPageSize: 10 });
response = (await iterator.next()).value;

// Prints 10 blob names
for (const blob of response.segment.blobItems) {
  console.log(`Blob ${i++}: ${blob.name}`);
}
setAccessPolicy(PublicAccessType, SignedIdentifier[], ContainerSetAccessPolicyOptions)

设置指定容器的权限。 权限指示是否可以公开访问容器中的 Blob。 为容器设置权限时,将替换现有权限。 如果未提供任何访问权限或 containerAcl,则会删除现有的容器 ACL。

在容器上建立存储访问策略时,最长可能需要 30 秒才能生效。 在此时间间隔内,与存储访问策略关联的共享访问签名将失败并显示状态代码 403(禁止访问),直到访问策略变为活动状态。

setMetadata(Metadata, ContainerSetMetadataOptions)

为指定的容器设置一个或多个用户定义的名称值对。 如果未提供任何选项,或者参数中未定义任何元数据,则会删除容器元数据。

uploadBlockBlob(string, HttpRequestBody, number, BlockBlobUploadOptions)

创建新的块 blob,或更新现有块 Blob 的内容。 更新现有块 Blob 会覆盖 Blob 上的任何现有元数据。 不支持部分更新;现有 Blob 的内容将被新内容覆盖。 若要执行块 blob 的部分更新,请使用 <xref:BlockBlobClient.stageBlock> 和 <xref:BlockBlobClient.commitBlockList>。

这是一种非并行上传方法,请使用 <xref:BlockBlobClient.uploadFile>、<xref:BlockBlobClient.uploadStream> 或 <xref:BlockBlobClient.uploadBrowserData>,以提高并发上传的性能。

构造函数详细信息

ContainerClient(string, PipelineLike)

创建 ContainerClient 的实例。 此方法接受指向容器的 URL。 编码的 URL 字符串不会转义两次,只会转义 URL 路径中的特殊字符。 如果 Blob 名称包含? 或 %,blob 名称必须在 URL 中编码。

new ContainerClient(url: string, pipeline: PipelineLike)

参数

url

string

指向 Azure 存储容器的 URL 字符串,例如“https://myaccount.blob.core.windows.net/mycontainer"。 如果使用 AnonymousCredential(如“https://myaccount.blob.core.windows.net/mycontainer?sasString"),则可以追加 SAS。

pipeline
PipelineLike

调用 newPipeline()以创建默认管道,或提供自定义管道。

ContainerClient(string, StorageSharedKeyCredential | AnonymousCredential | TokenCredential, StoragePipelineOptions)

创建 ContainerClient 的实例。 此方法接受指向容器的 URL。 编码的 URL 字符串不会转义两次,只会转义 URL 路径中的特殊字符。 如果 Blob 名称包含? 或 %,blob 名称必须在 URL 中编码。

new ContainerClient(url: string, credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential, options?: StoragePipelineOptions)

参数

url

string

指向 Azure 存储容器的 URL 字符串,例如“https://myaccount.blob.core.windows.net/mycontainer"。 如果使用 AnonymousCredential(如“https://myaccount.blob.core.windows.net/mycontainer?sasString"),则可以追加 SAS。

credential

StorageSharedKeyCredential | AnonymousCredential | TokenCredential

例如 AnonymousCredential、StorageSharedKeyCredential 或任何来自 @azure/identity 包的凭据,用于对服务的请求进行身份验证。 还可以提供实现 TokenCredential 接口的对象。 如果未指定,则使用 AnonymousCredential。

options
StoragePipelineOptions

自选。 用于配置 HTTP 管道的选项。

ContainerClient(string, string, StoragePipelineOptions)

创建 ContainerClient 的实例。

new ContainerClient(connectionString: string, containerName: string, options?: StoragePipelineOptions)

参数

connectionString

string

帐户连接字符串或 Azure 存储帐户的 SAS 连接字符串。 [ 注意 - 帐户连接字符串只能在NODE.JS运行时使用。 ] 帐户连接字符串示例 - DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=accountKey;EndpointSuffix=core.windows.net SAS 连接字符串示例 - BlobEndpoint=https://myaccount.blob.core.windows.net/;QueueEndpoint=https://myaccount.queue.core.windows.net/;FileEndpoint=https://myaccount.file.core.windows.net/;TableEndpoint=https://myaccount.table.core.windows.net/;SharedAccessSignature=sasString

containerName

string

容器名称。

options
StoragePipelineOptions

自选。 用于配置 HTTP 管道的选项。

属性详细信息

accountName

accountName: string

属性值

string

containerName

容器的名称。

string containerName

属性值

string

credential

例如 AnonymousCredential、StorageSharedKeyCredential 或任何来自 @azure/identity 包的凭据,用于对服务的请求进行身份验证。 还可以提供实现 TokenCredential 接口的对象。 如果未指定,则使用 AnonymousCredential。

credential: StorageSharedKeyCredential | AnonymousCredential | TokenCredential

属性值

url

编码的 URL 字符串值。

url: string

属性值

string

方法详细信息

create(ContainerCreateOptions)

在指定的帐户下创建新容器。 如果已存在同名的容器,作将失败。

function create(options?: ContainerCreateOptions)

参数

options
ContainerCreateOptions

容器创建作的选项。

示例用法:

const containerClient = blobServiceClient.getContainerClient("<container name>");
const createContainerResponse = await containerClient.create();
console.log("Container was created successfully", createContainerResponse.requestId);

返回

createIfNotExists(ContainerCreateOptions)

在指定的帐户下创建新容器。 如果已存在同名的容器,则不会更改。

function createIfNotExists(options?: ContainerCreateOptions)

参数

返回

delete(ContainerDeleteMethodOptions)

标记要删除的指定容器。 容器及其中包含的任何 blob 稍后将在垃圾回收期间删除。

function delete(options?: ContainerDeleteMethodOptions)

参数

options
ContainerDeleteMethodOptions

容器删除作的选项。

返回

deleteBlob(string, ContainerDeleteBlobOptions)

标记要删除的指定 Blob 或快照。 稍后在垃圾回收期间删除该 Blob。 请注意,若要删除 Blob,必须删除其所有快照。 可以使用“删除 Blob”作同时删除两者。

function deleteBlob(blobName: string, options?: ContainerDeleteBlobOptions)

参数

blobName

string

options
ContainerDeleteBlobOptions

Blob 删除作的选项。

返回

块 blob 删除响应数据。

deleteIfExists(ContainerDeleteMethodOptions)

标记指定的容器以供删除(如果存在)。 容器及其中包含的任何 blob 稍后将在垃圾回收期间删除。

function deleteIfExists(options?: ContainerDeleteMethodOptions)

参数

options
ContainerDeleteMethodOptions

容器删除作的选项。

返回

exists(ContainerExistsOptions)

如果此客户端表示的 Azure 容器资源存在,则返回 true;否则为 false。 注意:请谨慎使用此函数,因为其他客户端或应用程序可能会删除现有容器。 反之亦然,此函数完成后,其他客户端或应用程序可能会添加具有相同名称的新容器。

function exists(options?: ContainerExistsOptions)

参数

返回

Promise<boolean>

generateSasUrl(ContainerGenerateSasUrlOptions)

仅适用于使用共享密钥凭据构造的 ContainerClient。 基于传入的客户端属性和参数生成 Blob 容器服务共享访问签名(SAS)URI。 SAS 由客户端的共享密钥凭据签名。

function generateSasUrl(options: ContainerGenerateSasUrlOptions)

参数

options
ContainerGenerateSasUrlOptions

可选参数。

返回

Promise<string>

由此客户端表示的资源的 URI 组成的 SAS URI,后跟生成的 SAS 令牌。

getAccessPolicy(ContainerGetAccessPolicyOptions)

获取指定容器的权限。 权限指示是否可以公开访问容器数据。 警告:分析 startOn 和 expiresOn 字符串时,JavaScript 日期可能会丢失精度。 例如,新日期(“2018-12-31T03:44:23.8827891Z”)将获取“2018-12-31T03:44:23.882Z”。

function getAccessPolicy(options?: ContainerGetAccessPolicyOptions)

参数

options
ContainerGetAccessPolicyOptions

容器获取访问策略作的选项。

返回

getAppendBlobClient(string)

创建 <xref:AppendBlobClient>

function getAppendBlobClient(blobName: string)

参数

blobName

string

追加 Blob 名称

返回

getBlobBatchClient()

创建 BlobBatchClient 对象以执行批处理作。

function getBlobBatchClient()

返回

此容器的新 BlobBatchClient 对象。

getBlobClient(string)

创建 <xref:BlobClient>

function getBlobClient(blobName: string)

参数

blobName

string

Blob 名称

返回

给定 Blob 名称的新 BlobClient 对象。

getBlobLeaseClient(string)

获取管理容器租约的 <xref:BlobLeaseClient>。

function getBlobLeaseClient(proposeLeaseId?: string)

参数

proposeLeaseId

string

初始建议的租约 ID。

返回

一个新的 BlobLeaseClient 对象,用于管理容器上的租约。

getBlockBlobClient(string)

创建 <xref:BlockBlobClient>

function getBlockBlobClient(blobName: string)

参数

blobName

string

块 Blob 名称

示例用法:

const content = "Hello world!";

const blockBlobClient = containerClient.getBlockBlobClient("<blob name>");
const uploadBlobResponse = await blockBlobClient.upload(content, content.length);

返回

getPageBlobClient(string)

创建 <xref:PageBlobClient>

function getPageBlobClient(blobName: string)

参数

blobName

string

页 Blob 名称

返回

getProperties(ContainerGetPropertiesOptions)

返回指定容器的所有用户定义的元数据和系统属性。 返回的数据不包括容器的 Blob 列表。

function getProperties(options?: ContainerGetPropertiesOptions)

参数

options
ContainerGetPropertiesOptions

容器获取属性作的选项。

返回

listBlobsByHierarchy(string, ContainerListBlobsOptions)

返回异步可迭代迭代器以按层次结构列出所有 Blob。 在指定的帐户下。 .byPage() 返回异步可迭代迭代迭代器,以按页中的层次结构列出 Blob。

使用 for await 语法的示例:

for await (const item of containerClient.listBlobsByHierarchy("/")) {
  if (item.kind === "prefix") {
    console.log(`\tBlobPrefix: ${item.name}`);
  } else {
    console.log(`\tBlobItem: name - ${item.name}, last modified - ${item.properties.lastModified}`);
  }
}

使用 iter.next()的示例:

let iter = containerClient.listBlobsByHierarchy("/", { prefix: "prefix1/" });
let entity = await iter.next();
while (!entity.done) {
  let item = entity.value;
  if (item.kind === "prefix") {
    console.log(`\tBlobPrefix: ${item.name}`);
  } else {
    console.log(`\tBlobItem: name - ${item.name}, last modified - ${item.properties.lastModified}`);
  }
  entity = await iter.next();
}

使用 byPage()的示例:

console.log("Listing blobs by hierarchy by page");
for await (const response of containerClient.listBlobsByHierarchy("/").byPage()) {
  const segment = response.segment;
  if (segment.blobPrefixes) {
    for (const prefix of segment.blobPrefixes) {
      console.log(`\tBlobPrefix: ${prefix.name}`);
    }
  }
  for (const blob of response.segment.blobItems) {
    console.log(`\tBlobItem: name - ${blob.name}, last modified - ${blob.properties.lastModified}`);
  }
}

使用具有最大页面大小的分页的示例:

console.log("Listing blobs by hierarchy by page, specifying a prefix and a max page size");

let i = 1;
for await (const response of containerClient.listBlobsByHierarchy("/", { prefix: "prefix2/sub1/"}).byPage({ maxPageSize: 2 })) {
  console.log(`Page ${i++}`);
  const segment = response.segment;

  if (segment.blobPrefixes) {
    for (const prefix of segment.blobPrefixes) {
      console.log(`\tBlobPrefix: ${prefix.name}`);
    }
  }

  for (const blob of response.segment.blobItems) {
    console.log(`\tBlobItem: name - ${blob.name}, last modified - ${blob.properties.lastModified}`);
  }
}
function listBlobsByHierarchy(delimiter: string, options?: ContainerListBlobsOptions)

参数

delimiter

string

用于定义虚拟层次结构的字符或字符串

options
ContainerListBlobsOptions

列出 Blob作的选项。

返回

PagedAsyncIterableIterator<Object & BlobPrefix | Object & BlobItem, ContainerListBlobHierarchySegmentResponse>

listBlobsFlat(ContainerListBlobsOptions)

返回异步可迭代迭代器以列出指定帐户下的所有 Blob。 .byPage() 返回异步可迭代迭代器以列出页面中的 blob。

使用 for await 语法的示例:

// Get the containerClient before you run these snippets,
// Can be obtained from `blobServiceClient.getContainerClient("<your-container-name>");`
let i = 1;
for await (const blob of containerClient.listBlobsFlat()) {
  console.log(`Blob ${i++}: ${blob.name}`);
}

使用 iter.next()的示例:

let i = 1;
let iter = containerClient.listBlobsFlat();
let blobItem = await iter.next();
while (!blobItem.done) {
  console.log(`Blob ${i++}: ${blobItem.value.name}`);
  blobItem = await iter.next();
}

使用 byPage()的示例:

// passing optional maxPageSize in the page settings
let i = 1;
for await (const response of containerClient.listBlobsFlat().byPage({ maxPageSize: 20 })) {
  for (const blob of response.segment.blobItems) {
    console.log(`Blob ${i++}: ${blob.name}`);
  }
}

对标记使用分页的示例:

let i = 1;
let iterator = containerClient.listBlobsFlat().byPage({ maxPageSize: 2 });
let response = (await iterator.next()).value;

// Prints 2 blob names
for (const blob of response.segment.blobItems) {
  console.log(`Blob ${i++}: ${blob.name}`);
}

// Gets next marker
let marker = response.continuationToken;

// Passing next marker as continuationToken

iterator = containerClient.listBlobsFlat().byPage({ continuationToken: marker, maxPageSize: 10 });
response = (await iterator.next()).value;

// Prints 10 blob names
for (const blob of response.segment.blobItems) {
  console.log(`Blob ${i++}: ${blob.name}`);
}
function listBlobsFlat(options?: ContainerListBlobsOptions)

参数

options
ContainerListBlobsOptions

列出 Blob 的选项。

返回

PagedAsyncIterableIterator<BlobItem, ContainerListBlobFlatSegmentResponse>

支持分页的 asyncIterableIterator。

setAccessPolicy(PublicAccessType, SignedIdentifier[], ContainerSetAccessPolicyOptions)

设置指定容器的权限。 权限指示是否可以公开访问容器中的 Blob。 为容器设置权限时,将替换现有权限。 如果未提供任何访问权限或 containerAcl,则会删除现有的容器 ACL。

在容器上建立存储访问策略时,最长可能需要 30 秒才能生效。 在此时间间隔内,与存储访问策略关联的共享访问签名将失败并显示状态代码 403(禁止访问),直到访问策略变为活动状态。

function setAccessPolicy(access?: PublicAccessType, containerAcl?: SignedIdentifier[], options?: ContainerSetAccessPolicyOptions)

参数

access
PublicAccessType

对容器中的数据的公共访问级别。

containerAcl

SignedIdentifier[]

每个元素的数组具有唯一 ID 和访问策略的详细信息。

options
ContainerSetAccessPolicyOptions

容器集访问策略作的选项。

返回

setMetadata(Metadata, ContainerSetMetadataOptions)

为指定的容器设置一个或多个用户定义的名称值对。 如果未提供任何选项,或者参数中未定义任何元数据,则会删除容器元数据。

function setMetadata(metadata?: Metadata, options?: ContainerSetMetadataOptions)

参数

metadata
Metadata

将现有元数据替换为此值。 如果未提供任何值,将删除现有元数据。

options
ContainerSetMetadataOptions

容器集元数据作的选项。

返回

uploadBlockBlob(string, HttpRequestBody, number, BlockBlobUploadOptions)

创建新的块 blob,或更新现有块 Blob 的内容。 更新现有块 Blob 会覆盖 Blob 上的任何现有元数据。 不支持部分更新;现有 Blob 的内容将被新内容覆盖。 若要执行块 blob 的部分更新,请使用 <xref:BlockBlobClient.stageBlock> 和 <xref:BlockBlobClient.commitBlockList>。

这是一种非并行上传方法,请使用 <xref:BlockBlobClient.uploadFile>、<xref:BlockBlobClient.uploadStream> 或 <xref:BlockBlobClient.uploadBrowserData>,以提高并发上传的性能。

function uploadBlockBlob(blobName: string, body: HttpRequestBody, contentLength: number, options?: BlockBlobUploadOptions)

参数

blobName

string

要创建或更新的块 Blob 的名称。

body

HttpRequestBody

Blob、字符串、ArrayBuffer、ArrayBufferView 或返回一个新的可读流,其偏移量从数据源开始。

contentLength

number

正文长度(以字节为单位)。 使用 Buffer.byteLength() 计算字符串的正文长度,包括非 Base64/Hex 编码字符。

options
BlockBlobUploadOptions

用于配置块 Blob 上传作的选项。

返回

Promise<Object>

块 Blob 上传响应数据和相应的 BlockBlobClient 实例。