IDocumentClient.CreateAttachmentAsync Method

Definition

Overloads

CreateAttachmentAsync(String, Object, RequestOptions, CancellationToken)

Creates an attachment as an asychronous operation in the Azure Cosmos DB service.

CreateAttachmentAsync(Uri, Object, RequestOptions, CancellationToken)

Creates an attachment as an asychronous operation in the Azure Cosmos DB service.

CreateAttachmentAsync(String, Stream, MediaOptions, RequestOptions, CancellationToken)

Creates an Attachment with the contents of the provided mediaStream as an asynchronous operation in the Azure Cosmos DB service.

CreateAttachmentAsync(Uri, Stream, MediaOptions, RequestOptions, CancellationToken)

Creates an attachment as an asynchronous operation in the Azure Cosmos DB service.

CreateAttachmentAsync(String, Object, RequestOptions, CancellationToken)

Creates an attachment as an asychronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>> CreateAttachmentAsync (string documentLink, object attachment, Microsoft.Azure.Documents.Client.RequestOptions options = null, System.Threading.CancellationToken cancellationToken = null);
abstract member CreateAttachmentAsync : string * obj * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>

Parameters

documentLink
String

The link of the parent document for this new attachment. E.g. dbs/db_rid/colls/col_rid/docs/doc_rid/

attachment
Object

The attachment object.

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

The Task object representing the service response for the asynchronous operation.

Examples

The example below creates a new document, and then creates a new attachment for that document


 dynamic d = new
 {
     id = "DOC1800243243470"
 };

 Document doc = await client.CreateDocumentAsync(collectionSelfLink, d);

 //Create an Attachment which links to binary content stored somewhere else
 //Use the MediaLink property of Attachment to set where the binary resides
 //MediaLink can also point at another Attachment within Azure Cosmos DB.
 Attachment a = await client.CreateAttachmentAsync(doc.SelfLink, new Attachment { Id = "foo", ContentType = "text/plain", MediaLink = "link to your media" });

 //Because Attachment is a Dynamic object you can use SetPropertyValue method to any property you like
 //Even if that property doesn't exist. Here we are creating two new properties on the Attachment we created above.
 a.SetPropertyValue("Foo", "some value");
 a.SetPropertyValue("Bar", "some value");

 //Now update the Attachment object in the database to persist the new properties on the object
 client.ReplaceAttachmentAsync(a);

 //Let's now create another Attachment except this time we're going to use a Dynamic object instead
 //of a <see cref="Microsoft.Azure.Documents.Attachment"/> as we did above.
 var b = await client.CreateAttachmentAsync(doc.SelfLink, new { id = "foo", contentType = "text/plain", media="link to your media", a = 5, b = 6 });

 //Now you will have a Document in your database with two attachments.
 

See also

CreateAttachmentAsync(Uri, Object, RequestOptions, CancellationToken)

Creates an attachment as an asychronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>> CreateAttachmentAsync (Uri documentUri, object attachment, Microsoft.Azure.Documents.Client.RequestOptions options = null, System.Threading.CancellationToken cancellationToken = null);
abstract member CreateAttachmentAsync : Uri * obj * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>

Parameters

documentUri
Uri

The URI of the document to create an attachment for.

attachment
Object

The attachment object.

options
RequestOptions

(Optional) The RequestOptions for the request.

cancellationToken
CancellationToken

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

Returns

The task object representing the service response for the asynchronous operation.

CreateAttachmentAsync(String, Stream, MediaOptions, RequestOptions, CancellationToken)

Creates an Attachment with the contents of the provided mediaStream as an asynchronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>> CreateAttachmentAsync (string attachmentsLink, System.IO.Stream mediaStream, Microsoft.Azure.Documents.Client.MediaOptions options = null, Microsoft.Azure.Documents.Client.RequestOptions requestOptions = null, System.Threading.CancellationToken cancellationToken = null);
abstract member CreateAttachmentAsync : string * System.IO.Stream * Microsoft.Azure.Documents.Client.MediaOptions * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>

Parameters

attachmentsLink
String

The attachments link for the document. E.g. dbs/db_rid/colls/col_rid/docs/doc_rid/attachments/

mediaStream
Stream

the Stream of the attachment media.

options
MediaOptions

the MediaOptions for the request.

requestOptions
RequestOptions

Request options.

cancellationToken
CancellationToken

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

Returns

The task object representing the service response for the asynchronous operation.

Exceptions

If either attachmentsLink or mediaStream is not set.

Examples


  //This attachment could be any binary you want to attach. Like images, videos, word documents, pdfs etc. it doesn't matter
  using (FileStream fileStream = new FileStream(@".\something.pdf", FileMode.Open))
  {
      //Create the attachment
      Attachment attachment = await client.CreateAttachmentAsync("dbs/db_rid/colls/coll_rid/docs/doc_rid/attachments/",
                                          fileStream,
                                          new MediaOptions
                                          {
                                              ContentType = "application/pdf",
                                              Slug = "something.pdf"
                                          });
  }

See also

CreateAttachmentAsync(Uri, Stream, MediaOptions, RequestOptions, CancellationToken)

Creates an attachment as an asynchronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>> CreateAttachmentAsync (Uri documentUri, System.IO.Stream mediaStream, Microsoft.Azure.Documents.Client.MediaOptions options = null, Microsoft.Azure.Documents.Client.RequestOptions requestOptions = null, System.Threading.CancellationToken cancellationToken = null);
abstract member CreateAttachmentAsync : Uri * System.IO.Stream * Microsoft.Azure.Documents.Client.MediaOptions * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Attachment>>

Parameters

documentUri
Uri

The URI of the document to create an attachment for.

mediaStream
Stream

The stream of the attachment media.

options
MediaOptions

The media options for the request.

requestOptions
RequestOptions

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

The task object representing the service response for the asynchronous operation.

Applies to