如果我只有要上传的文件的 URL,如何在 azure 中上传文件。在本例中,我使用 Dropbox file chooser它从 dropbox 中选择文件并返回其 url 路径。 等式
https://www.dropbox.com/s/o9myet72y19iaan/Getting%20Started.pdf
现在我们需要将文件存储在 Windows Azure blob 中。无需先下载文件,最简单的方法是什么? 我计划使用 asp.net Web api 将文件上传到 azure blob。
最佳答案
起初,我认为这应该非常简单,因为 Azure Blob 存储支持从外部 URL 复制 blob,但我认为这不适用于 Dropbox 文件。我刚刚尝试了一下,但还是出现了错误。
您上面提到的链接不是该文件的直接链接。这是 Dropbox 网站页面的链接,您可以从该页面下载文件。这显然是你不想要的。您可以尝试以下替代解决方案:
替换www.dropbox.com
在您的网址中包含 dl.dropboxusercontent.com
(基于下面 @smarx 的评论)并在以下代码中使用该 URL:
首先您需要附加 dl=1
将您的请求 URL 作为查询字符串。因此您的 Dropbox URL 将是 https://www.dropbox.com/s/o9myet72y19iaan/Getting%20Started.pdf?dl=1
。 dl
查询字符串参数表示需要下载的文件。
接下来,使用 HTTPWebRequest
尝试访问此 URL。 Dropbox 将回复另一个链接和 302
状态码。该链接类似于 https://dl.dropboxusercontent.com/s/o9myet72y19iaan/Getting%20Started.pdf?token_hash=<tokenhash>
.
使用下面代码中的此链接来复制文件。这会起作用。
CloudStorageAccount acc = new CloudStorageAccount(new StorageCredentials("account", "key"), false);
var client = acc.CreateCloudBlobClient();
var container = client.GetContainerReference("container-name");
container.CreateIfNotExists();
var blob = container.GetBlockBlobReference("dropbox-file-name");
blob.StartCopyFromBlob(new Uri("dropbox URL with dl.dropboxusercontent.com"));
Console.WriteLine("Copy request accepted");
Console.WriteLine("Now checking for copy state");
bool continueLoop = true;
do
{
blob.FetchAttributes();
var copyState = blob.CopyState;
switch (copyState.Status)
{
case CopyStatus.Pending:
Console.WriteLine("Copy is still pending. Will check status again after 1 second.");
System.Threading.Thread.Sleep(1000);//Copy is still pending...check after 1 second
break;
default:
Console.WriteLine("Terminating process with copy state = " + copyState.Status);
continueLoop = false;
break;
}
}
while (continueLoop);
Console.WriteLine("Press any key to continue.");
关于javascript - 将文件上传到 Windows Azure,仅提供文件的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23484650/