예제 #1
0
        private async Task <ulong> CreateBlobAsyncWorker(byte[] data, CancellationToken cancellationToken)
        {
            await TaskUtilities.SwitchToBackgroundThread();

            var request = await CreateBlobRequest.SendAsync(this, data, cancellationToken);

            return(await request.Task);
        }
예제 #2
0
        public static void Commit(string originmdPath, string modifiedPath, string repoName)
        {
            Console.WriteLine("******************Six steps for Commit***************************");
            //get reference & tree to commit
            string url_getRef    = url_Head + repoName + url_getRefTail;
            string url_getCommit = url_Head + repoName + url_getCommitTail;
            string parent_sha    = JObject.Parse(Get(url_getRef, new Dictionary <string, string>()))["object"]["sha"].ToString();
            string baseTree_sha  = JObject.Parse(Get(url_getCommit + parent_sha, new Dictionary <string, string>()))["tree"]["sha"].ToString();

            //create a  blob
            string            url_createBlob    = url_Head + repoName + url_createBlobTail;
            StreamReader      sr                = new StreamReader(originmdPath, Encoding.GetEncoding("utf-8"));
            CreateBlobRequest createBlobRequest = new CreateBlobRequest
            {
                Content  = sr.ReadToEnd(),
                Encoding = "utf-8"
            };
            string createBlobBody = JsonConvert.SerializeObject(createBlobRequest);
            string blob_sha       = JObject.Parse(Post(url_createBlob, createBlobBody))["sha"].ToString();

            //create a new tree for commit
            string            url_createTree    = url_Head + repoName + url_createTreeTail;
            CreateTreeRequest createTreeRequest = new CreateTreeRequest
            {
                BaseTree = baseTree_sha,
                Tree     = new TreeNode[] {
                    new TreeNode {
                        Path = modifiedPath,
                        Mode = "100644",
                        Type = "blob",
                        Sha  = blob_sha
                    }
                }
            };
            string createTreeBody = JsonConvert.SerializeObject(createTreeRequest);
            string treeSubmit_sha = JObject.Parse(Post(url_createTree, createTreeBody))["sha"].ToString();

            //create a  new commit
            string url_createCommit = url_Head + repoName + url_createCommitTail;
            CreateCommitRequest createCommitRequest = new CreateCommitRequest
            {
                Message = "Commit automatically!",
                Parents = new string[] { parent_sha },
                Tree    = treeSubmit_sha
            };
            string createCommitBody = JsonConvert.SerializeObject(createCommitRequest);
            string createSubmit_sha = JObject.Parse(Post(url_createCommit, createCommitBody))["sha"].ToString();

            //update reference
            string url_updateRef = url_Head + repoName + url_updateRefTail;
            UpdateReferenceRequest updateReferenceRequest = new UpdateReferenceRequest
            {
                Sha   = createSubmit_sha,
                Force = true
            };
            string updateReferenceBody = JsonConvert.SerializeObject(updateReferenceRequest);
            string updateRef_res       = Post(url_updateRef, updateReferenceBody).ToString();
        }
예제 #3
0
        /// <summary>
        /// Creates and uploads a blob.
        /// </summary>
        /// <param name="payload">the blob payload</param>
        /// <returns>id of the blob</returns>
        public Task <string> CreateBlob(Payload payload)
        {
            var request = new CreateBlobRequest {
                Payload = payload
            };

            return(gateway(authenticationContext()).CreateBlobAsync(request)
                   .ToTask(response => response.BlobId));
        }
예제 #4
0
            public static async Task <CreateBlobRequest> CreateAsync(RHost host, CancellationToken cancellationToken)
            {
                var message = host.CreateRequestMessage("?CreateBlob", new JArray());
                var request = new CreateBlobRequest(host, message, cancellationToken);

                await host.SendAsync(message, cancellationToken);

                return(request);
            }
예제 #5
0
        private static void Commit()
        {
            Console.WriteLine("******************Six steps for Commit***************************");
            //get reference & tree to commit
            string parent_sha   = JObject.Parse(Get(url_getRef, new Dictionary <string, string>()))["object"]["sha"].ToString();
            string baseTree_sha = JObject.Parse(Get(url_getCommit + parent_sha, new Dictionary <string, string>()))["tree"]["sha"].ToString();

            //create a  blob
            StreamReader      sr = new StreamReader(sourceFile, Encoding.GetEncoding("utf-8"));
            CreateBlobRequest createBlobRequest = new CreateBlobRequest
            {
                Content  = sr.ReadToEnd(),
                Encoding = "utf-8"
            };
            string createBlobBody = JsonConvert.SerializeObject(createBlobRequest);
            string blob_sha       = JObject.Parse(Post(url_createBlob, createBlobBody))["sha"].ToString();

            //create a new tree for commit
            CreateTreeRequest createTreeRequest = new CreateTreeRequest
            {
                BaseTree = baseTree_sha,
                Tree     = new TreeNode[] {
                    new TreeNode {
                        Path = @"node-azure-tools.md",
                        Mode = "100644",
                        Type = "blob",
                        Sha  = blob_sha
                    }
                }
            };
            string createTreeBody = JsonConvert.SerializeObject(createTreeRequest);
            string treeSubmit_sha = JObject.Parse(Post(url_createTree, createTreeBody))["sha"].ToString();

            //create a  new commit
            CreateCommitRequest createCommitRequest = new CreateCommitRequest
            {
                Message = "Commit automatically!",
                Parents = new string[] { parent_sha },
                Tree    = treeSubmit_sha
            };
            string createCommitBody = JsonConvert.SerializeObject(createCommitRequest);
            string createSubmit_sha = JObject.Parse(Post(url_createCommit, createCommitBody))["sha"].ToString();

            //update reference
            UpdateReferenceRequest updateReferenceRequest = new UpdateReferenceRequest
            {
                Sha   = createSubmit_sha,
                Force = true
            };
            string updateReferenceBody = JsonConvert.SerializeObject(updateReferenceRequest);
            string updateRef_res       = Post(url_updateRef, updateReferenceBody).ToString();
        }
예제 #6
0
        public async Task <CreateBlobResponse> CreateBlobAsync(CreateBlobRequest request)
        {
            var blobId = Guid.NewGuid().ToString();

            var fileBytes = Convert.FromBase64String(request.Base64File);

            _cloudBlockBlob = _cloudBlobContainer.GetBlockBlobReference(BuildReference(blobId, request.Extension));

            await _cloudBlockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length);

            return(new CreateBlobResponse()
            {
                Path = _cloudBlockBlob.StorageUri.PrimaryUri.AbsoluteUri,
                BlobId = blobId,
            });
        }
예제 #7
0
        public async Task <ulong> CreateBlobAsync(CancellationToken cancellationToken)
        {
            if (_runTask == null)
            {
                throw new InvalidOperationException("Host was not started");
            }

            using (CancellationTokenUtilities.Link(ref cancellationToken, _cts.Token)) {
                try {
                    await TaskUtilities.SwitchToBackgroundThread();

                    var request = await CreateBlobRequest.CreateAsync(this, cancellationToken);

                    return(await request.Task);
                } catch (OperationCanceledException ex) when(_cts.IsCancellationRequested)
                {
                    throw new RHostDisconnectedException(Resources.Error_RHostIsStopped, ex);
                }
            }
        }
        public async Task <Result> CreateBlobAsync(CreateBlobRequest request)
        {
            try
            {
                var blobServiceClient = new BlobServiceClient(_storageAccountConfiguration.ConnectionString);

                var blobContainerClient = blobServiceClient.GetBlobContainerClient(request.Container);

                // Create the BLOB container if it does not exist
                await blobContainerClient.CreateIfNotExistsAsync();


                var blobClient = blobContainerClient.GetBlobClient(request.FileName);

                var memoryStream = new MemoryStream();
                var streamWriter = new StreamWriter(memoryStream)
                {
                    AutoFlush = true
                };

                await streamWriter.WriteAsync(request.Content);

                memoryStream.Position = 0;

                var uploadResponse = await blobClient.UploadAsync(memoryStream, true, CancellationToken.None);

                var isCreated = uploadResponse?.GetRawResponse()?.Status == (int)HttpStatusCode.Created;

                return(isCreated ? Result.Success() : Result.Failure("Error when creating the blob."));
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error when creating the BLOB");
            }

            return(Result.Failure("Error when creating the blob."));
        }