Пример #1
0
        private async static Task QueryWithAttachments(DocumentClient client)
        {
            Document document = client
                                .CreateDocumentQuery("dbs/mydb/colls/mystore", "SELECT * FROM c WHERE c.id = 'ALBUM152'")
                                .AsEnumerable()
                                .First();

            var attachments = client
                              .CreateAttachmentQuery(document.SelfLink)
                              .ToList();

            foreach (var attachment in attachments)
            {
                var response = await client.ReadMediaAsync(attachment.MediaLink);

                var bytes = new byte[response.ContentLength];
                await response.Media.ReadAsync(bytes, 0, (int)response.ContentLength);

                var filename = string.Format(@"C:\Demo\Images\Attachment{0}.jpg", attachment.ResourceId);
                using (var fs = new FileStream(filename, FileMode.CreateNew))
                {
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
        }
        private static async Task UseAttachments(string colSelfLink)
        {
            dynamic documentWithAttachment = new
            {
                id         = "PO1800243243470",
                CustomerId = 1092,
                TotalDue   = 985.018m,
            };

            Document doc = await client.CreateDocumentAsync(colSelfLink, documentWithAttachment);

            //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(@".\Attachments\text.txt", FileMode.Open))
            {
                //Create the attachment
                await client.CreateAttachmentAsync(doc.AttachmentsLink, fileStream, new MediaOptions { ContentType = "text/plain", Slug = "text.txt" });
            }

            //Query the documents for attachments
            Attachment attachment = client.CreateAttachmentQuery(doc.SelfLink).AsEnumerable().FirstOrDefault();

            //Use DocumentClient to read the Media content
            MediaResponse content = await client.ReadMediaAsync(attachment.MediaLink);

            byte[] bytes = new byte[content.ContentLength];
            await content.Media.ReadAsync(bytes, 0, (int)content.ContentLength);

            string result = Encoding.UTF8.GetString(bytes);
        }
Пример #3
0
        private static async Task MigrateSingleCollection(
            DocumentClient client,
            Database database,
            string cosmosCollectionName,
            string ravenCollectionName,
            JsonTextWriter jsonTextWriter,
            StreamWriter streamWriter)
        {
            var collection = UriFactory.CreateDocumentCollectionUri(database.Id, cosmosCollectionName);
            var options    = new FeedOptions
            {
                MaxItemCount = 128
            };

            var query            = client.CreateDocumentQuery <ExpandoObject>(collection, options).AsDocumentQuery();
            var attachmentNumber = 0;

            while (query.HasMoreResults)
            {
                var result = await query.ExecuteNextAsync <ExpandoObject>();

                foreach (var document in result)
                {
                    var dictionary = (IDictionary <string, object>)document;
                    var documentId = dictionary[CosmosDocumentId].ToString();

                    const string selfLinkKey = "_self";
                    var          selfLink    = dictionary[selfLinkKey].ToString();

                    var attachments = new List <Dictionary <string, object> >();
                    foreach (var attachment in client.CreateAttachmentQuery(selfLink))
                    {
                        var attachmentUri      = UriFactory.CreateAttachmentUri(database.Id, cosmosCollectionName, documentId, attachment.Id);
                        var attachmentResponse = await client.ReadAttachmentAsync(attachmentUri);

                        var resourceMediaLink = attachmentResponse.Resource.MediaLink;
                        var mediaLinkResponse = await client.ReadMediaAsync(resourceMediaLink);

                        attachmentNumber++;

                        var attachmentInfo = await MigrationHelpers.WriteAttachment(
                            mediaLinkResponse.Media, mediaLinkResponse.Media.Length,
                            attachmentResponse.Resource.Id, ravenCollectionName,
                            mediaLinkResponse.ContentType, attachmentNumber, jsonTextWriter, streamWriter);

                        attachments.Add(attachmentInfo);
                    }

                    MigrationHelpers.WriteDocument(
                        document,
                        documentId,
                        ravenCollectionName,
                        PropertiesToRemove,
                        jsonTextWriter,
                        attachments);
                }
            }
        }
Пример #4
0
        private static void Main(string[] args)
        {
            ConnectionPolicy policy = new ConnectionPolicy()
            {
                ConnectionMode     = ConnectionMode.Direct,
                ConnectionProtocol = Protocol.Tcp
            };
            Uri endPoint = new Uri(EndpointUrl);

            using (DocumentClient client = new DocumentClient(endPoint, AuthKey, policy))
            {
                Database           database   = client.CreateDatabaseQuery().Where(db => db.Id == DatabasebId).AsEnumerable().First();
                DocumentCollection collection =
                    client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == CollectionId).AsEnumerable().First();
                DataClass data1 = new DataClass()
                {
                    Name     = "Name 1",
                    UniqueId = 1,
                    Date     = DateTime.UtcNow
                };
                DataClass data2 = new DataClass()
                {
                    Name     = "Name 2",
                    UniqueId = 2,
                    Date     = DateTime.UtcNow
                };
                Task <ResourceResponse <Document> > td1 = client.CreateDocumentAsync(collection.DocumentsLink, data1);
                Task <ResourceResponse <Document> > td2 = client.CreateDocumentAsync(collection.DocumentsLink, data2);
                Task.WaitAll(td1, td2);
                Document doc1       = td1.Result.Resource;
                Document doc2       = td1.Result.Resource;
                Stream   attStream1 = File.Open(FileName1, FileMode.Open, FileAccess.Read, FileShare.Read);
                Stream   attStream2 = File.Open(FileName2, FileMode.Open, FileAccess.Read, FileShare.Read);
                client.CreateAttachmentAsync(doc1.AttachmentsLink, attStream1);
                client.CreateAttachmentAsync(doc2.AttachmentsLink, attStream2);
                DataClass[] data        = client.CreateDocumentQuery <DataClass>(collection.DocumentsLink).Select(d => d).ToArray();
                var         attachments = client.CreateAttachmentQuery(doc1.AttachmentsLink).ToArray();
            }
        }
Пример #5
0
        public async Task <byte[]> GetRawMail(DSMail source)
        {
            var docUri = UriFactory.CreateDocumentUri(DTO.Constants.ConnectionSettings.DOCDB_DATABASE_NAME,
                                                      DTO.Constants.ConnectionSettings.DOCDB_COLLECTION_NAME, source.id);
            var response = await Client.ReadDocumentAsync(docUri);

            string attachmentLink = response.Resource.AttachmentsLink;
            var    attachment     = Client.CreateAttachmentQuery(attachmentLink)
                                    .Where(a => a.Id == source.MessageId)
                                    .AsEnumerable()
                                    .FirstOrDefault();

            if (attachment == null)
            {
                throw new ApplicationException("Not Found");
            }
            var mediaResponse = await Client.ReadMediaAsync(attachment.MediaLink);

            byte[] rawMail = new byte[mediaResponse.ContentLength];
            await mediaResponse.Media.ReadAsync(rawMail, 0, (int)mediaResponse.ContentLength);

            return(rawMail);
        }