Exemple #1
0
        internal async Task SaveParsedMail(ParsedMail parsedMail)
        {
            Uri messageUri = UriFactory.CreateDocumentCollectionUri(DTO.Constants.ConnectionSettings.DOCDB_DATABASE_NAME,
                                                                    DTO.Constants.ConnectionSettings.DOCDB_COLLECTION_NAME);

            using (var client = new DocumentClient(new Uri(docDBendPoint), docDBKey))
            {
                foreach (var message in parsedMail.Mail)
                {
                    message.RawMailSize = parsedMail.RawMail.Length;
                    var response = await client.CreateDocumentAsync(messageUri, message);

                    await client.CreateAttachmentAsync(response.Resource.AttachmentsLink, new MemoryStream(parsedMail.RawMail),
                                                       new MediaOptions { ContentType = "application/octect-stream", Slug = message.MessageId });

                    if (!parsedMail.Attachments.Any())
                    {
                        continue;
                    }

                    foreach (var attachment in parsedMail.Attachments)
                    {
                        await client.CreateAttachmentAsync(response.Resource.AttachmentsLink, new MemoryStream(attachment.File),
                                                           new MediaOptions { ContentType = attachment.ContentType, Slug = attachment.Name });
                    }
                }
            }
        }
Exemple #2
0
        public static async Task <Attachment> CreateAttachmentAsync(string id, object attachment)
        {
            Document d = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));

            Attachment a = await client.CreateAttachmentAsync(d.SelfLink, attachment);

            return(a);
        }
        async void myMenuItemAttachmentFromFile_Click(object sender, EventArgs eventArg)
        {
            var ofd = new OpenFileDialog();
            var dr  = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                var filename = ofd.FileName;
                //
                // todo: present the dialog for Slug name and Content type
                //
                Program.GetMain().SetLoadingState();

                try
                {
                    using (var stream = new FileStream(filename,
                                                       FileMode.Open, FileAccess.Read))
                    {
                        var mediaOptions = new MediaOptions()
                        {
                            ContentType = "application/octet-stream",
                            Slug        = Path.GetFileName(ofd.FileName)
                        };

                        ResourceResponse <Attachment> rr;

                        var document   = ((Document)Tag);
                        var collection = ((DocumentCollection)Parent.Tag);

                        var requestOptions = GetRequestOptions();
                        if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0)
                        {
                            requestOptions.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey));
                        }

                        using (PerfStatus.Start("CreateAttachment"))
                        {
                            rr = await _client.CreateAttachmentAsync((Tag as Document).SelfLink + "/attachments",
                                                                     stream, mediaOptions, requestOptions);
                        }

                        var json = rr.Resource.ToString();

                        SetResultInBrowser(json, null, false, rr.ResponseHeaders);

                        Nodes.Add(new ResourceNode(_client, rr.Resource, ResourceType.Attachment));
                    }
                }
                catch (Exception e)
                {
                    SetResultInBrowser(null, e.ToString(), true);
                }
            }
        }
Exemple #4
0
        public async Task <Document> CreateDocumentAsync(T item, List <BlobDetails> lstBlobDetailses)
        {
            try
            {
                var response =
                    await _client.CreateDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId), item);

                if (response.StatusCode != HttpStatusCode.Created)
                {
                    return(response);
                }
                var attachmentUrl = string.Concat(response.Resource.AltLink, "/attachments/");

                foreach (var itm in lstBlobDetailses)
                {
                    var data = new
                    {
                        id          = itm.Name,
                        media       = itm.BlobUri,
                        contentType = itm.ContentType
                    };

                    await _client.CreateAttachmentAsync(attachmentUrl, data);
                }

                return(response);
            }
            catch (DocumentClientException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(null);
        }
 public async Task <ResourceResponse <Attachment> > CreateAttachmentAsync(
     string attachmentsLink,
     object attachment,
     RequestOptions options)
 {
     return(await Client.CreateAttachmentAsync(attachmentsLink, attachment, options));
 }
        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);
        }
Exemple #7
0
        private void CreateDocumentAttachment(Document document, Stream messageStream)
        {
            var mediaOptions = new MediaOptions
            {
                ContentType = "application/pdf",
                Slug        = "something.pdf"
            };

            _client.CreateAttachmentAsync(document.SelfLink, messageStream, mediaOptions);
        }
Exemple #8
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();
            }
        }
        private async static Task <Document> CreateWithAttachments(DocumentClient client)
        {
            dynamic documentDefinition = new
            {
                id            = "ALBUM152",
                name          = "New Customer 99",
                favoriteAlbum = "The worst nightmare",
                tags          = new[]
                {
                    "arctics",
                    "monkeys"
                }
            };

            Document document = await client.CreateDocumentAsync("dbs/mydb/colls/mystore", documentDefinition);

            Console.WriteLine("Created document");
            Console.WriteLine(document);

            using (var fs = new FileStream(@"C:\Demo\piwi.jpg", FileMode.Open))
            {
                var result = await client.CreateAttachmentAsync(document.AttachmentsLink, fs);

                Console.WriteLine("Created attachment #1");
                Console.WriteLine(result.Resource);
            }

            using (var fs = new FileStream(@"C:\Demo\mug.jpg", FileMode.Open))
            {
                var result = await client.CreateAttachmentAsync(document.AttachmentsLink, fs);

                Console.WriteLine("Created attachment #2");
                Console.WriteLine(result.Resource);
            }

            return(document);
        }
Exemple #10
0
        private async static Task <Document> CreateWithAttachments(DocumentClient client)
        {
            dynamic documentDefinition = new
            {
                id        = "ALBUM152",
                name      = "New Customer 1",
                albumName = "My favorite pictures",
                tags      = new[]
                {
                    "cats",
                    "mountains"
                }
            };

            Document document = await client.CreateDocumentAsync("dbs/mydb/colls/mystore", documentDefinition);

            Console.WriteLine("Created document");
            Console.WriteLine(document);

            using (var fs = new FileStream(@"C:\Demo\Mufasa.jpg", FileMode.Open))
            {
                var result = await client.CreateAttachmentAsync(document.AttachmentsLink, fs);

                Console.WriteLine("Created attachment #1");
                Console.WriteLine(result.Resource);
            }

            using (var fs = new FileStream(@"C:\Demo\Ascent.jpg", FileMode.Open))
            {
                var result = await client.CreateAttachmentAsync(document.AttachmentsLink, fs);

                Console.WriteLine("Created attachment #2");
                Console.WriteLine(result.Resource);
            }

            return(document);
        }
Exemple #11
0
        private static async Task UploadAttachmentsAsync()
        {
            DirectoryInfo sourceDirectory = new DirectoryInfo("source");

            Console.WriteLine("Upserting parent documents ...");

            IList <Task> createParentDocumentTasks = new List <Task>();

            foreach (FileInfo file in sourceDirectory.EnumerateFiles())
            {
                createParentDocumentTasks.Add(
                    cosmosClient.UpsertDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri(cosmosDatabaseName, cosmosCollectionName),
                        new { id = file.Name }
                        )
                    );
            }

            await Task.WhenAll(createParentDocumentTasks);

            Console.WriteLine("Uploading attachments ...");

            IList <Task> uploadAttachmentTasks = new List <Task>();
            int          totalCount            = 0;

            foreach (FileInfo file in sourceDirectory.EnumerateFiles())
            {
                Console.WriteLine("Scheduled task to upload file: {0}", file.Name);
                uploadAttachmentTasks.Add(
                    cosmosClient.CreateAttachmentAsync(
                        UriFactory.CreateDocumentUri(cosmosDatabaseName, cosmosCollectionName, file.Name),
                        new FileStream(file.FullName, FileMode.Open, FileAccess.Read),
                        new MediaOptions {
                    ContentType = "application/octet-stream", Slug = file.Name
                },
                        new RequestOptions {
                    PartitionKey = new PartitionKey(file.Name)
                }
                        )
                    );
                totalCount++;
            }

            await Task.WhenAll(uploadAttachmentTasks);

            Console.WriteLine("Finished uploading {0} attachments", totalCount);
        }