ListBlobsWithPrefix() публичный Метод

Returns an enumerable collection of blob items whose names begin with the specified prefix.
public ListBlobsWithPrefix ( string prefix ) : IEnumerable
prefix string The blob name prefix. This value must be preceded either by the name of the container or by the absolute URI to the container.
Результат IEnumerable
        private void MoveDirectory(CloudBlobClient blobClient, string newPrefix, string oldPrefix)
        {
            var blobs = blobClient.ListBlobsWithPrefix(oldPrefix,
                new BlobRequestOptions() { BlobListingDetails = Microsoft.WindowsAzure.StorageClient.BlobListingDetails.Metadata, UseFlatBlobListing = false });
            foreach (var blob in blobs)
            {
                if (blob is CloudBlobDirectory)
                {
                    var dir = blob as CloudBlobDirectory;

                    var names = dir.Uri.ToString().Split('/');
                    for (var i = names.Length - 1; i >= 0; i--)
                    {
                        if (!string.IsNullOrEmpty(names[i]))
                        {
                            MoveDirectory(blobClient, newPrefix + StorageNamesEncoder.EncodeBlobName(names[i]) + "/", oldPrefix + StorageNamesEncoder.EncodeBlobName(names[i]) + "/");
                            break;
                        }
                    }
                }
                else if (blob is CloudBlob)
                {
                    var cloudBlob = blob as CloudBlob;

                    if (cloudBlob.Exists())
                    {
                        cloudBlob.FetchAttributes();
                        var newContentBlob = blobClient.GetBlockBlobReference(newPrefix + cloudBlob.Metadata["FileName"]);
                        try
                        {
                            newContentBlob.CopyFromBlob(cloudBlob);
                        }
                        catch (Exception e)
                        {
                            using (Stream stream = new MemoryStream())
                            {
                                cloudBlob.DownloadToStream(stream);
                                stream.Position = 0;
                                newContentBlob.UploadFromStream(stream);
                                stream.Dispose();
                            }
                        }
                        newContentBlob.Metadata["FileName"] = cloudBlob.Metadata["FileName"];

                        if (!string.IsNullOrEmpty(cloudBlob.Metadata["UserId"]))
                        {
                            newContentBlob.Metadata["UserId"] = cloudBlob.Metadata["UserId"];
                        }
                        if (!string.IsNullOrEmpty(cloudBlob.Metadata["Published"]))
                        {
                            newContentBlob.Metadata["Published"] = cloudBlob.Metadata["Published"];
                        }
                        if (!string.IsNullOrEmpty(cloudBlob.Metadata["Size"]))
                        {
                            newContentBlob.Metadata["Size"] = cloudBlob.Metadata["Size"];
                        }
                        if (cloudBlob.Metadata.AllKeys.Contains("AlternateText"))
                        {
                            newContentBlob.Metadata["AlternateText"] = cloudBlob.Metadata["AlternateText"];
                        }
                        if (cloudBlob.Metadata.AllKeys.Contains("Description"))
                        {
                            newContentBlob.Metadata["Description"] = cloudBlob.Metadata["Description"];
                        }
                        if (cloudBlob.Metadata.AllKeys.Contains("Title"))
                        {
                            newContentBlob.Metadata["Title"] = cloudBlob.Metadata["Title"];
                        }

                        newContentBlob.SetMetadata();
                        cloudBlob.DeleteIfExists();
                    }
                }
            }
        }
        private void zipFolder(CloudBlobClient blobClient, string basePrefix, string folderName, string zipDir, ref ZipFile zipFile)
        {
            zipDir = string.IsNullOrEmpty(zipDir) ? folderName : zipDir + "/" + folderName;
            zipFile.AddDirectoryByName(zipDir);
            var folderPrefix = basePrefix + StorageNamesEncoder.EncodeBlobName(folderName) + "/";

            var blobs = blobClient.ListBlobsWithPrefix(folderPrefix,
            new BlobRequestOptions() { BlobListingDetails = Microsoft.WindowsAzure.StorageClient.BlobListingDetails.Metadata, UseFlatBlobListing = false });
            foreach (var blob in blobs)
            {
                if (blob is CloudBlobDirectory)
                {
                    var dir = blob as CloudBlobDirectory;

                    var names = dir.Uri.ToString().Split('/');
                    for (var i = names.Length - 1; i >= 0; i--)
                    {
                        if (!string.IsNullOrEmpty(names[i]))
                        {
                            zipFolder(blobClient, folderPrefix, StorageNamesEncoder.DecodeBlobName(names[i]), zipDir, ref zipFile);
                            break;
                        }
                    }
                }
                if (blob is CloudBlob)
                {
                    var cloudBlob = blob as CloudBlob;
                    var subStr = cloudBlob.Uri.ToString().Substring(cloudBlob.Uri.ToString().IndexOf(folderPrefix) + folderPrefix.Length);
                    var index = subStr.LastIndexOf('/');
                    if (index < 0)
                    {
                        index = 0;
                    }

                    var subFolderName = subStr.Substring(0, index);
                    var fileName = subStr.Substring(index);
                    var bytes = cloudBlob.DownloadByteArray();
                    if (!string.IsNullOrEmpty(subFolderName))
                    {
                        zipFile.AddDirectoryByName(zipDir + "/" + StorageNamesEncoder.DecodeBlobName(subFolderName));
                    }
                    zipFile.AddEntry(zipDir + "/" + StorageNamesEncoder.DecodeBlobName(subStr), bytes);
                }
            }
        }
Пример #3
0
        private void MoveDirectory(CloudBlobClient blobClient, string newPrefix, string oldPrefix)
        {
            var blobs = blobClient.ListBlobsWithPrefix(oldPrefix,
                new BlobRequestOptions() { BlobListingDetails = Microsoft.WindowsAzure.StorageClient.BlobListingDetails.Metadata, UseFlatBlobListing = false });
            foreach (var blob in blobs)
            {
                if (blob is CloudBlobDirectory)
                {
                    var dir = blob as CloudBlobDirectory;

                    var names = dir.Uri.ToString().Split('/');
                    for (var i = names.Length - 1; i >= 0; i--)
                    {
                        if (!string.IsNullOrEmpty(names[i]))
                        {
                            MoveDirectory(blobClient, newPrefix + names[i] + "/", oldPrefix + names[i] + "/");
                            break;
                        }
                    }
                }
                else if (blob is CloudBlob)
                {
                    var cloudBlob = blob as CloudBlob;

                    if (cloudBlob.Exists())
                    {
                        cloudBlob.FetchAttributes();
                        var newContentBlob = blobClient.GetBlockBlobReference(newPrefix + cloudBlob.Metadata["FileName"]);
                        newContentBlob.CopyFromBlob(cloudBlob);
                        newContentBlob.Metadata["FileName"] = cloudBlob.Metadata["FileName"];
                        newContentBlob.SetMetadata();
                        cloudBlob.DeleteIfExists();
                    }
                }
            }
        }