Esempio n. 1
0
        public async Task <bool> PushItemToFeed(string item, string relativePath, SemaphoreSlim clientThrottle, bool allowOverwrite)
        {
            try
            {
                string uploadPath       = feed.CalculateBlobPath(item, relativePath);
                string packageDirectory = feed.CalculateRelativeUploadPath(item, relativePath);

                if (!allowOverwrite && await feed.CheckIfBlobExists(uploadPath))
                {
                    throw new Exception($"Item {uploadPath} already exists and Overwrite is false.");
                }
                await UploadAsync(CancellationToken, item, uploadPath, clientThrottle, allowOverwrite);

                List <string> listAzureBlobs = await ListAzureBlobs.ListBlobs(Log, feed.AccountName, feed.AccountKey, feed.ContainerName, packageDirectory);

                if (!listAzureBlobs.Any(x => x.Contains(uploadPath)))
                {
                    throw new Exception($"Uploaded package {uploadPath} is not present on feed. Cannot update index.json.");
                }

                await UploadIndexJson(clientThrottle, true, packageDirectory, listAzureBlobs);
            }
            catch (Exception e)
            {
                Log.LogErrorFromException(e);
            }

            return(!Log.HasLoggedErrors);
        }
Esempio n. 2
0
        public async Task UploadAssets(ITaskItem item, SemaphoreSlim clientThrottle, bool allowOverwrite = false)
        {
            string relativeBlobPath = item.GetMetadata("RelativeBlobPath");

            if (string.IsNullOrEmpty(relativeBlobPath))
            {
                string fileName     = Path.GetFileName(item.ItemSpec);
                string recursiveDir = item.GetMetadata("RecursiveDir");
                relativeBlobPath = $"{feed.RelativePath}{recursiveDir}{fileName}";
            }

            relativeBlobPath = relativeBlobPath.Replace("\\", "/");

            Log.LogMessage($"Uploading {relativeBlobPath}");

            await clientThrottle.WaitAsync();

            try
            {
                bool blobExists = false;

                if (!allowOverwrite)
                {
                    blobExists = await feed.CheckIfBlobExists(relativeBlobPath);
                }

                if (allowOverwrite || !blobExists)
                {
                    Log.LogMessage($"Uploading {item} to {relativeBlobPath}.");
                    UploadClient uploadClient = new UploadClient(Log);
                    await uploadClient.UploadBlockBlobAsync(
                        CancellationToken,
                        feed.AccountName,
                        feed.AccountKey,
                        feed.ContainerName,
                        item.ItemSpec,
                        relativeBlobPath);
                }
                else
                {
                    Log.LogError($"Item '{item}' already exists in {relativeBlobPath}.");
                }
            }
            catch (Exception exc)
            {
                Log.LogError($"Unable to upload to {relativeBlobPath} due to {exc}.");
                throw;
            }
            finally
            {
                clientThrottle.Release();
            }
        }
Esempio n. 3
0
        private async Task UploadAsync(CancellationToken ct, string item, string uploadPath, SemaphoreSlim clientThrottle, bool allowOverwrite)
        {
            if (!File.Exists(item))
            {
                throw new Exception(string.Format("The file '{0}' does not exist.", item));
            }

            await clientThrottle.WaitAsync();

            string         leaseId   = string.Empty;
            AzureBlobLease blobLease = new AzureBlobLease(feed.AccountName, feed.AccountKey, string.Empty, feed.ContainerName, uploadPath, Log, "60", "10");

            bool isLeaseRequired = allowOverwrite && await feed.CheckIfBlobExists(uploadPath);

            if (isLeaseRequired)
            {
                try
                {
                    leaseId = blobLease.Acquire();
                }
                catch (Exception)
                {
                    Log.LogError($"Unable to obtain lease on {uploadPath}");
                }
            }
            try
            {
                bool isExists = await feed.CheckIfBlobExists(uploadPath);

                if (!isExists || allowOverwrite)
                {
                    Log.LogMessage($"Uploading {item} to {uploadPath}.");
                    UploadClient uploadClient = new UploadClient(Log);
                    await
                    uploadClient.UploadBlockBlobAsync(
                        ct,
                        feed.AccountName,
                        feed.AccountKey,
                        feed.ContainerName,
                        item,
                        uploadPath,
                        leaseId);
                }
                else
                {
                    Log.LogMessage($"Skipping uploading of {item} to {uploadPath}. Already exists.");
                }
            }
            catch (Exception)
            {
                Log.LogError($"Unable to upload to {uploadPath}");
            }
            finally
            {
                if (isLeaseRequired)
                {
                    blobLease.Release();
                }
                clientThrottle.Release();
            }
        }