Ejemplo n.º 1
0
        public async Task UploadIfNeeded(CloudStorageAccount account, string containerName, string blobName, FileInfo fileInfo)
        {
            var            client    = account.CreateCloudBlobClient();
            var            container = client.GetContainerReference(containerName);
            CloudBlockBlob blob      = container.GetBlockBlobReference(blobName);

            var b = await GetContentInfo(blob);

            var f = await GetContentInfo(fileInfo);

            var result = ((Func <BlobComparisonResult>)(() =>
            {
                if (b == ContentInfo.NotExist)
                {
                    return(BlobComparisonResult.NotEqual);
                }
                if (b.Length != f.Length)
                {
                    return(BlobComparisonResult.NotEqual);
                }
                if (string.IsNullOrEmpty(b.ContentMD5))
                {
                    return(BlobComparisonResult.SizeSameContentUnclear);
                }
                if (!string.Equals(b.ContentMD5, f.ContentMD5))
                {
                    return(BlobComparisonResult.NotEqual);
                }
                return(BlobComparisonResult.PrettySureEqual);
            }))();

            if (result == BlobComparisonResult.NotEqual ||
                result == BlobComparisonResult.SizeSameContentUnclear)
            {
                await LargeFileUploaderUtils.UploadAsync(
                    fetchLocalData : (offset, length) => fileInfo.GetFileContentAsync(offset, length),
                    blobLenth : fileInfo.Length,
                    storageAccount : account,
                    containerName : containerName,
                    blobName : blobName);

                blob.Properties.ContentMD5 = f.ContentMD5;
                await blob.SetPropertiesAsync();

                blob.Metadata["LastModified"] = fileInfo.LastWriteTimeUtc.ToString();
                await blob.SetMetadataAsync();

                log(string.Format("Uploaded {0} to {1}", fileInfo.FullName, blob.Uri.AbsoluteUri));
            }
            else
            {
                log(string.Format("Skipped {0}", fileInfo.FullName));
            }
        }
        public bool Execute()
        {
            Action <string> log = msg => BuildEngine.LogMessageEvent(new BuildMessageEventArgs(
                                                                         message: msg, helpKeyword: string.Empty, senderName: this.GetType().Name,
                                                                         importance: MessageImportance.High));

            var connectionString = File.ReadAllText(this.ConnectionStringFile);
            var account          = CloudStorageAccount.Parse(connectionString);
            var client           = account.CreateCloudBlobClient();
            var container        = client.GetContainerReference(this.ContainerName);

            Func <FileInfo, System.Threading.Tasks.Task> uploadAsync = async(inputFile) =>
            {
                await container.CreateIfNotExistsAsync();

                LargeFileUploader.LargeFileUploaderUtils.Log = log;

                await LargeFileUploaderUtils.UploadAsync(
                    file : inputFile,
                    storageAccount : account,
                    containerName : this.ContainerName,
                    uploadParallelism : 1);
            };

            Func <string, System.Threading.Tasks.Task <DateTime> > getLastModifiedAsync = async(blobName) =>
            {
                try
                {
                    var blobReference = await container.GetBlobReferenceFromServerAsync(blobName);

                    if (!blobReference.Exists())
                    {
                        return(DateTime.MinValue);
                    }
                    if (!blobReference.Metadata.ContainsKey("LastModified"))
                    {
                        return(DateTime.MinValue);
                    }
                    var  lastModifiedStr = blobReference.Metadata["LastModified"];
                    long timeTicks       = long.Parse(lastModifiedStr);
                    var  lastModified    = new DateTime(timeTicks, DateTimeKind.Utc);
                    return(lastModified);
                }
                catch (StorageException)
                {
                    return(DateTime.MinValue);
                }
            };

            Func <string, FileInfo, System.Threading.Tasks.Task> setLastModifiedAsync = async(blobName, fileInfo) =>
            {
                var blobReference = await container.GetBlobReferenceFromServerAsync(blobName);

                if (!blobReference.Exists())
                {
                    return;
                }
                blobReference.Metadata["LastModified"] = fileInfo.LastWriteTimeUtc.Ticks.ToString();
                blobReference.SetMetadata();
                blobReference.Properties.ContentType = ContentType;
                if (!String.IsNullOrWhiteSpace(ContentEncoding))
                {
                    blobReference.Properties.ContentEncoding = ContentEncoding;
                }
                await blobReference.SetPropertiesAsync();
            };


            var uploadTasks = Files.Select(async(fileItem) => {
                FileInfo file   = new FileInfo(fileItem.ItemSpec);
                string blobName = (string.IsNullOrEmpty(DestinationFolder) ? "" : string.Format("{0}/", DestinationFolder)) + file.Name;

                DateTime lastModified = await getLastModifiedAsync(blobName);
                if (lastModified != file.LastWriteTimeUtc)
                {
                    log("must upload");
                    await uploadAsync(file);
                    await setLastModifiedAsync(blobName, file);

                    log(string.Format("Updating: {0}", file.Name));
                }
            }).ToArray();

            System.Threading.Tasks.Task.WhenAll(uploadTasks).Wait();


            return(true);
        }