我创建了一个Azure存储帐户。我有一个400兆字节的.zip文件,我想将其放入Blob存储中以供以后使用。
我如何在不编写代码的情况下完成这个任务?有没有相应的界面可以实现呢?
我创建了一个Azure存储帐户。我有一个400兆字节的.zip文件,我想将其放入Blob存储中以供以后使用。
我如何在不编写代码的情况下完成这个任务?有没有相应的界面可以实现呢?
免费工具:
曾经有一个名为Azure Blob Explorer的旧程序,但它已经无法与新的Azure SDK配合使用。
在这些工具中,我个人最喜欢CloudBerry Explorer。
最简单的方法是使用Azure Storage PowerShell。它提供了许多命令来管理您的存储容器/块/表/队列。
对于您提到的情况,您可以使用Set-AzureStorageBlobContent命令将本地文件上传到Azure存储作为块Blob或页Blob。
Set-AzureStorageBlobContent -Container containerName -File .\filename -Blob blobname
详细信息请参见 http://msdn.microsoft.com/en-us/library/dn408487.aspx。
StorageClient已经内置了此功能,无需编写任何内容:
var account = new CloudStorageAccount(creds, false);
var client = account.CreateCloudBlobClient();
var blob = client.GetBlobReference("/somecontainer/hugefile.zip");
//1MB seems to be a pretty good all purpose size
client.WriteBlockSizeInBytes = 1024;
//this sets # of parallel uploads for blocks
client.ParallelOperationThreadCount = 4; //normally set to one per CPU core
//this will break blobs up automatically after this size
client.SingleBlobUploadThresholdInBytes = 4096;
blob.UploadFile("somehugefile.zip");
微软提供了一个新的开源工具:
请查看以下链接:
您可以使用命令提示符将文件上传到Azure存储帐户Blob。
安装Microsoft Azure Storage工具。
然后使用CLI命令将其上传到您的帐户blob:
AzCopy /Source:"filepath" /Dest:bloburl /DestKey:accesskey /destType:blob
AzCopy
工具的简单批处理文件就可以解决问题了。您可以将文件拖放到以下批处理文件中,以上传到您的 Blob 存储容器中:
upload.bat
@ECHO OFF
SET BLOB_URL=https://<<<account name>>>.blob.core.windows.net/<<<container name>>>
SET BLOB_KEY=<<<your access key>>>
:AGAIN
IF "%~1" == "" GOTO DONE
AzCopy /Source:"%~d1%~p1" /Dest:%BLOB_URL% /DestKey:%BLOB_KEY% /Pattern:"%~n1%~x1" /destType:blob
SHIFT
GOTO AGAIN
:DONE
PAUSE
Pattern
标志),而不是上传整个目录。您可以使用HTTP PUT动词将大型文件直接上传到Azure Blob Storage,下面的代码是我尝试过的最大文件大小为4.6 GB。您可以使用以下C#代码完成此操作:
// write up to ChunkSize of data to the web request
void WriteToStreamCallback(IAsyncResult asynchronousResult)
{
var webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
var requestStream = webRequest.EndGetRequestStream(asynchronousResult);
var buffer = new Byte[4096];
int bytesRead;
var tempTotal = 0;
File.FileStream.Position = DataSent;
while ((bytesRead = File.FileStream.Read(buffer, 0, buffer.Length)) != 0
&& tempTotal + bytesRead < CHUNK_SIZE
&& !File.IsDeleted
&& File.State != Constants.FileStates.Error)
{
requestStream.Write(buffer, 0, bytesRead);
requestStream.Flush();
DataSent += bytesRead;
tempTotal += bytesRead;
File.UiDispatcher.BeginInvoke(OnProgressChanged);
}
requestStream.Close();
if (!AbortRequested) webRequest.BeginGetResponse(ReadHttpResponseCallback, webRequest);
}
void StartUpload()
{
var uriBuilder = new UriBuilder(UploadUrl);
if (UseBlocks)
{
// encode the block name and add it to the query string
CurrentBlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
uriBuilder.Query = uriBuilder.Query.TrimStart('?') + string.Format("&comp=block&blockid={0}", CurrentBlockId);
}
// with or without using blocks, we'll make a PUT request with the data
var webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(uriBuilder.Uri);
webRequest.Method = "PUT";
webRequest.BeginGetRequestStream(WriteToStreamCallback, webRequest);
}
UploadUrl是由Azure自动生成的,其中包含一个共享访问签名。这个SAS URL指定了要上传Blob的位置以及授予安全访问(在您的情况下为写入访问)的时间长度。您可以像这样生成SAS URL:
readonly CloudBlobClient BlobClient;
readonly CloudBlobContainer BlobContainer;
public UploadService()
{
// Setup the connection to Windows Azure Storage
var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
BlobClient = storageAccount.CreateCloudBlobClient();
// Get and create the container
BlobContainer = BlobClient.GetContainerReference("publicfiles");
}
string JsonSerializeData(string url)
{
var serializer = new DataContractJsonSerializer(url.GetType());
var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, url);
return Encoding.Default.GetString(memoryStream.ToArray());
}
public string GetUploadUrl()
{
var sasWithIdentifier = BlobContainer.GetSharedAccessSignature(new SharedAccessPolicy
{
Permissions = SharedAccessPermissions.Write,
SharedAccessExpiryTime =
DateTime.UtcNow.AddMinutes(60)
});
return JsonSerializeData(BlobContainer.Uri.AbsoluteUri + "/" + Guid.NewGuid() + sasWithIdentifier);
}
我在这个主题上也有一个帖子,你可以在这里找到更多信息 如何从网页上传大文件到Azure Blob