Share via


Create an Attachment

 

There are two ways to create an attachment resource – post the media content to DocumentDB like in the AtomPub Protocol, or post just the attachment metadata to media stored externally.

The first is to POST the raw media in the body payload to store it in the provided attachment storage under your DocumentDB account. For the attachment storage quota, see DocumentDB limits. To create this type of attachment, developers include the raw attachment (video, audio, file, blob, etc.) as the body of the POST. Two headers must be set: Content-Type and Slug. The Content-Type header is set to the MIME type of the attachment while the Slug header is set to the name of the attachment.

The second way to create an attachment resource is to POST the attachment resource properties noting the type and media link of the attachment. Unlike the first type of attachment resource, the Content-Type and Slug headers are not to be set as the raw media is not part of the request payload.

Request

Request Syntax

Method

Request URI

HTTP Version

POST

https://{databaseaccount}.documents.azure.com/dbs/{_rid-db}/colls/{_rid-col}/docs/{_rid-doc}/attachments.

HTTP/1.1

Note that the {databaseaccount} is the name of the DocumentDB account created under your subscription. The {_rid-db} value is the resource ID of the database. The {_rid-col} value is the resource ID of the collection the document is contained in. The {_rid-doc} value is the document associated with the attachment.

Request Headers

Standard Header

Description

Authorization

Required. The authentication type and signature token. Only the master key token is allowed for this operation. For more information, see Access Control on DocumentDB Resources.

Content-Type

Optional. The content type of the attachment. This is only required when raw media is submitted to the DocumentDB attachment storage. For more details, refer to AtomPub Protocol.

Slug

Optional. The name of the attachment. This is only required when raw media is submitted to the DocumentDB attachment storage. For more details, refer to AtomPub Protocol.

User-Agent

Optional. The client user agent performing the request. The recommended format is {user agent name}/{version}. For example, the DocumentDB .NET SDK sets the User-Agent string to Microsoft.Document.Client/1.0.0.0.

Custom Header

Description

x-ms-date

Required. The date of the request, per RFC 1123. The date is expressed in Coordinated Universal Time format, e.g. Fri, 08 Apr 2015 03:52:31 GMT.

x-ms-version

Optional. The version of the DocumentDB REST service. The latest version is used when the header is not provided. For more information, see Versioning for DocumentDB REST API.

x-ms-indexing-directive

Optional. The acceptable value is Include or Exclude.

  • Include includes the attachment from indexing

  • Exclude omits the document from indexing. Raw media or attachment bits are not indexed or searchable.

Request Body

Property

Description

id

Not Required to be set when attaching raw media. This is a user settable property. It is the unique name that identifies the attachment, i.e. no two attachments will share the same id. The id must not exceed 255 characters.

contentType

Not Required to be set when attaching raw media. This is a user settable property. It notes the content type of the attachment. When submitting the attachment bits as body, DocumentDB sets the contentType to the type set in the Content-Type header.

Media

Not Required to be set when attaching raw media. This is the URL link or file path where the attachment resides.

Request Example(s)

Sample Request: Attachment with Raw Media

This sample shows that the raw media submitted is part of the POST payload. The Content-Type header is set to image/png and the Slug header to brush.png.

POST https://contosomarketing.documents.azure.com/dbs/hUwBcw==/colls/hUwBc+gfDX4=/docs/hUwBc+gfDX4DAAAAAAAAAA==/attachments HTTP/1.1
x-ms-date: Thu, 14 Apr 2015 22:40:25 GMT
authorization: type%3dmaster%26ver%3d1.0%26sig%3dza46lCo9nNr0%2fGMjryG8S%2b26ZsFABUYPlW3ebq26nDg%3d
x-ms-version: 2015-04-08
Content-Type: image/png
Slug: brush.png
Content-Length: 244

Sample Request: Attachment without Raw Media (only metadata)

This sample shows the JSON body containing the metadata of the attachment resource, and specifies the name, type and link for the raw media. Note that the Content-Type and Slug headers are not set.

POST https://contosomarketing.documents.azure.com/dbs/hUwBcw==/colls/hUwBc+gfDX4=/docs/hUwBc+gfDX4DAAAAAAAAAA==/attachments HTTP/1.1
x-ms-date: Thu, 14 Apr 2015 22:40:25 GMT
authorization: type%3dmaster%26ver%3d1.0%26sig%3dza46lCo9nNr0%2fGMjryG8S%2b26ZsFABUYPlW3ebq26nDg%3d
x-ms-version: 2015-04-08
Content-Length: 88

{"id":"Sample3_coverpageimage__v2","contentType":"image/jpg","media":"www.bing.com"}

Response

Response Headers

This operation returns the following common headers. There may be additional standard and common headers returned.

Standard Header

Description

Content-Type

The Content-Type is application/json. DocumentDB always return the response body in standard JSON format.

etag

This etag carries the same value as the _etag value returned as part of the response body.

Date

The date time of the response operation. This date time format conforms to the RFC 1123 date time format expressed in Coordinated Universal Time.

Custom Header

Description

x-ms-max-media-storage-usage-mb

When using DocumentDB attachment storage to store raw media, this header shows the total allotted attachment storage quota for the account. For more information on limits and quotas, see DocumentDB limits.

x-ms-media-storage-usage-mb

When using DocumentDB attachment storage to store raw media, this header shows the current attachment storage consumed.

x-ms-request-charge

This is The number of request units (RU) consumed by the operation. For more information about request units, see Manage DocumentDB capacity and performance.

x-ms-activity-id

This is the unique identifier for the operation. It is used for trouble-shooting purposes.

Response Body

Creating an attachment results in the creation of an attachment resource with the following response body.

Property

Description

id

This is the unique name that identifies the attachment. The value set in Slug is recorded here.

contentType

The content type of the attachment. The Content-Type value is recorded here when raw media is submitted.

Media

This is the URL link or file path where the attachment resides.

_rid

This is a system generated property. The resource ID (_rid) is a unique identifier that is also hierarchical per the resource stack on the resource model. It is used internally for placement and navigation of the attachment resource.

_ts

This is a system generated property. It specifies the last updated timestamp of the resource. The value is a timestamp.

_self

This is a system generated property. It is the unique addressable URI for the resource.

_etag

This is a system generated property that specifies the resource etag required for optimistic concurrency control.

Response Status Codes

The following table lists common status codes returned by this operation. For information related to error status codes, see HTTP Status Codes.

Code

Description

201 Created

The operation was successful.

400 Bad Request

The JSON body is invalid. Check for missing curly brackets or quotes.

401 Unauthorized

The Authorization or x-ms-date header is not set. 401 is also returned when the Authorization header is set to an invalid authorization token. For more information on authorization token, see Access Control on DocumentDB Resources.

403 Forbidden

The authorization token has expired. 403 is also returned by this operation when the number of permissions in the account has reached its allowable quota. For more information on limits and quota, see DocumentDB limits.

409 Conflict

The id or Slug provided for the new attachment has been taken by an existing attachment.

413 Entity Too Large

The document size in the request exceeded the allowable document size in a request. For more information on document size limits, see DocumentDB limits.

Response Example

Sample Response: Attachment with Raw Media

Note that the media property indicates the media link where the raw media can be retrieved from the DocumentDB attachment storage. The Slug value is recorded in the id property while the Content-Type value is recorded in the contentType property.

HTTP/1.1 201 Created
Transfer-Encoding: chunked
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
x-ms-max-media-storage-usage-mb: 2048
x-ms-media-storage-usage-mb: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin,Slug,Content-Type,Accept
x-ms-last-state-change-utc: Thu, 14 Apr 2015 18:14:58.157 GMT
etag: 00002a00-0000-0000-0000-53ed3ad90000
x-ms-resource-quota: documentSize=256;
x-ms-resource-usage: documentsSize=0;
x-ms-schemaversion: 1.1
x-ms-request-charge: 3.08
x-ms-indexing-directive: Default
x-ms-serviceversion: version=0.9.0.0
x-ms-activity-id: 8ee85ee2-afec-464d-bfd7-f6c5c4da9b83
x-ms-gatewayversion: version=0.9.0.0
Date: Thu, 14 Apr 2015 22:40:25 GMT

{
"contentType":"image\/png",
"id":"brush.png",
"media":"\/media\/DTsFCc8YD28DAAAAAAAAAEnfli8=",
"_rid":"DTsFCc8YD28DAAAAAAAAAEnfli8=",
"_ts":1401314635000,
"_self":"dbs\/DTsFCQ==\/colls\/DTsFCc8YD28=\/docs\/DTsFCc8YD28DAAAAAAAAAA==\/attachments\/DTsFCc8YD28DAAAAAAAAAEnfli8=",
"_etag":"00008d00-0000-0000-0000-53865d4b0000"
}

Sample Response: Attachment without Raw Media

The response is similar to the POST request for the raw media attachment aside from the fact that the media is stored somewhere else. Note that in this case, the brush.png file is stored at www.bing.com.

HTTP/1.1 201 Created
Transfer-Encoding: chunked
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
x-ms-max-media-storage-usage-mb: 2048
x-ms-media-storage-usage-mb: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin,Slug,Content-Type,Accept
x-ms-last-state-change-utc: Thu, 14 Apr 2015 18:14:58.157 GMT
etag: 00002a00-0000-0000-0000-53ed3ad90000
x-ms-resource-quota: documentSize=256;
x-ms-resource-usage: documentsSize=0;
x-ms-schemaversion: 1.1
x-ms-request-charge: 3.08
x-ms-indexing-directive: Default
x-ms-serviceversion: version=0.9.0.0
x-ms-activity-id: 8ee85ee2-afec-464d-bfd7-f6c5c4da9b83
x-ms-gatewayversion: version=0.9.0.0
Date: Thu, 14 Apr 2015 22:40:25 GMT

{
"contentType":"image\/png",
"id":"brush.png",
"media":"www.bing.com",
"_rid":"DTsFCc8YD28DAAAAAAAAAEnfli8=",
"_ts":1401314635000,
"_self":"dbs\/DTsFCQ==\/colls\/DTsFCc8YD28=\/docs\/DTsFCc8YD28DAAAAAAAAAA==\/attachments\/DTsFCc8YD28DAAAAAAAAAEnfli8=",
"_etag":"00008d00-0000-0000-0000-53865d4b0000"
}