我有一个azure函数,它将文件下载到临时文件路径,然后将临时文件保存到blob。我有两个问题:
1)是否有办法管理该功能创建的临时文件 Path.GetTempFileName()。即在函数范围之后删除临时文件名?
2)将文件从网址下载到blob是否有更好的做法?
var filename = Path.GetTempFileName();
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(url, filename);
using (var fileStream = System.IO.File.OpenRead(filename))
{
blob.UploadFromStream(fileStream);
}
答案 0 :(得分:1)
更有效的方法是将URL内容直接流式传输到blob流,而不是下载到本地文件系统。像这样:
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
using (WebResponse response = await request.GetResponseAsync())
using (Stream dataStream = response.GetResponseStream())
{
await blob.UploadFromStreamAsync(dataStream);
}
答案 1 :(得分:0)
实际上,您可以通过CloudBlob.StartCopy直接从源URL执行服务器端副本,而不是下载然后下载:
var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var blobClient = account.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference(destinationContainer);
blobContainer.CreateIfNotExists();
var newBlockBlob = blobContainer.GetBlockBlobReference(newFileName);
newBlockBlob.StartCopy(new Uri(sourceUrl));
// Monitor the copy state
while (true)
{
newBlockBlob.FetchAttributes();
Console.WriteLine("Copy state: {0}", newBlockBlob.CopyState);
if (newBlockBlob.CopyState.Status != CopyStatus.Pending)
{
break;
}
Thread.Sleep(waitTime);
}