上传文件到Windows Azure只提供了该文件的链接 [英] Upload file to Windows Azure with only the link of the file is provided

查看:259
本文介绍了上传文件到Windows Azure只提供了该文件的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何上传文件在蔚蓝的,如果我只有文件的URL上传。在这种情况下,我使用 Dropbox的文件选择器从Dropbox和选择文件返回它的URL路径。
EQ

How can I upload a file in azure if I only have the URL of the file to upload. In this case, i 'm using Dropbox file chooser which selects file from dropbox and returns its url path. eq

https://www.dropbox.com/s/o9myet72y19iaan/Getting% 20Started.pdf

现在我们需要存储在Windows天青一滴该文件。什么是这样做没有先下载该文件的最简单的方法。
我打算使用asp.net网站的API文件上传到蔚蓝色的斑点。

Now we need the file to be stored in Windows Azure blob. What is the easiest way to do this without downloading the file first. I'm planning to use a asp.net web api for the uploading of file to azure blob.

推荐答案

起初,我以为这应该是很简单的从外部URL的Azure Blob存储支持复制的斑点,但是我不认为这会在以下情况下工作Dropbox的文件。我只是尝试,并得到一个错误,即使

At first, I thought it should be quite straight forward as Azure Blob Storage support copying blobs from external URL however I don't think this would work in case of Dropbox files. I just tried it and got an error even though.

您上面提到的链接不直接链接到该文件。这是从那里你可以将文件下载到一个页面中的链接的Dropbox的网站。这显然​​是你不想要的。这里有一个替代的解决方案,你可以试试:

The link you mentioned above is not the direct link to the file. It's a link to a page on Dropbox's website from where you can download a file. This is obviously you don't want. Here's an alternate solution which you can try:

替换(根据@ smarx的评论 www.dropbox.com dl.dropboxusercontent.com 您的网址以下),使用该网址在以下code:

Replace www.dropbox.com in your URL with dl.dropboxusercontent.com (based on @smarx's comments below) and use that URL in the following code:

首先,你将需要追加 DL = 1 您的请求URL作为查询字符串。所以,你的Dropbox的网址是 https://www.dropbox.com/s/o9myet72y19iaan/Getting%20Started.pdf?dl=1 DL 查询字符串参数表示的文件需要下载。

First you would need to append dl=1 to your request URL as query string. So your Dropbox URL would be https://www.dropbox.com/s/o9myet72y19iaan/Getting%20Started.pdf?dl=1. dl query string parameter indicates the file needs to be downloaded.

接下来,使用的HttpWebRequest 尝试访问该网址。 Dropbox的响应会回来的另一个链接, 302 状态code。此链接会是这样的<$c$c>https://dl.dropboxusercontent.com/s/o9myet72y19iaan/Getting%20Started.pdf?token_hash=<tokenhash>.

Next, using HTTPWebRequest try accessing this URL. Dropbox will respond back with another link and 302 status code. This link would be something like https://dl.dropboxusercontent.com/s/o9myet72y19iaan/Getting%20Started.pdf?token_hash=<tokenhash>.

使用此链接在code以下复制文件。这会工作。

        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.");

这篇关于上传文件到Windows Azure只提供了该文件的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆