DocumentClient.ReadAttachmentAsync Method

Definition

Overloads

ReadAttachmentAsync(String, RequestOptions, CancellationToken)

Reads an Attachment from the Azure Cosmos DB service as an asynchronous operation.

ReadAttachmentAsync(Uri, RequestOptions, CancellationToken)

Reads an Attachment as an asynchronous operation from the Azure Cosmos DB service.

ReadAttachmentAsync(String, RequestOptions, CancellationToken)

Reads an Attachment from the Azure Cosmos DB service as an asynchronous operation.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>> ReadAttachmentAsync (string attachmentLink, Microsoft.Azure.Documents.Client.RequestOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadAttachmentAsync : string * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>
override this.ReadAttachmentAsync : string * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>
Public Function ReadAttachmentAsync (attachmentLink As String, Optional options As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResourceResponse(Of Attachment))

Parameters

attachmentLink
String

The link to the attachment to be read.

options
RequestOptions

(Optional) The request options for the request.

cancellationToken
CancellationToken

(Optional) A CancellationToken that can be used by other objects or threads to receive notice of cancellation.

Returns

A System.Threading.Tasks containing a ResourceResponse<TResource> which wraps a Attachment containing the read resource record.

Implements

Exceptions

If attachmentLink is not set.

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
404NotFound - This means the resource you tried to read did not exist.
429TooManyRequests - This means you have exceeded the number of request units per second. Consult the DocumentClientException.RetryAfter value to see how long you should wait before retrying this operation.

Examples

//Reads an Attachment resource where
// - sample_db is the ID property of the Database
// - sample_coll is the ID property of the DocumentCollection
// - sample_doc is the ID property of the Document
// - attachment_id is the ID property of the Attachment resource you wish to read.
var attachLink = "/dbs/sample_db/colls/sample_coll/docs/sample_doc/attachments/attachment_id/";
Attachment attachment = await client.ReadAttachmentAsync(attachLink);

Remarks

Doing a read of a resource is the most efficient way to get a resource from the service. If you know the resource's ID, do a read instead of a query by ID.

The example shown uses ID-based links, where the link is composed of the ID properties used when the resources were created. You can still use the SelfLink property of the Database if you prefer. A self-link is a URI for a resource that is made up of Resource Identifiers (or the _rid properties). ID-based links and SelfLink will both work. The format for attachmentLink is always "/dbs/{db identifier}/colls/{coll identifier}/docs/{doc identifier}/attachments/{attachment identifier}" only the values within the {} change depending on which method you wish to use to address the resource.

See also

Applies to

ReadAttachmentAsync(Uri, RequestOptions, CancellationToken)

Reads an Attachment as an asynchronous operation from the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>> ReadAttachmentAsync (Uri attachmentUri, Microsoft.Azure.Documents.Client.RequestOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadAttachmentAsync : Uri * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>
override this.ReadAttachmentAsync : Uri * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>
Public Function ReadAttachmentAsync (attachmentUri As Uri, Optional options As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResourceResponse(Of Attachment))

Parameters

attachmentUri
Uri

A URI to the Attachment resource to be read.

options
RequestOptions

The request options for the request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

A System.Threading.Tasks containing a ResourceResponse<TResource> which wraps an Attachment containing the read resource record.

Implements

Exceptions

If attachmentUri is not set.

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
404NotFound - This means the resource you tried to read did not exist.
429TooManyRequests - This means you have exceeded the number of request units per second. Consult the DocumentClientException.RetryAfter value to see how long you should wait before retrying this operation.

Examples

//Reads an Attachment resource where 
// - db_id is the ID property of the Database
// - coll_id is the ID property of the DocumentCollection
// - doc_id is the ID property of the Document
// - attachment_id is the ID property of the Attachment resource you wish to read. 
var attachLink = UriFactory.CreateAttachmentUri("db_id", "coll_id", "doc_id", "attachment_id");
Attachment attachment = await client.ReadAttachmentAsync(attachLink);

Remarks

Doing a read of a resource is the most efficient way to get a resource from the service. If you know the resource's ID, do a read instead of a query by ID.

See also

Applies to