FineUploader错误获取Azure Blob存储URI

时间:2017-12-23 19:05:42

标签: azure fine-uploader azure-blob-storage

我正在尝试将FineUploader React库应用到我的React应用程序中,以将文件上传到我的Azure Blob存储。

看起来由于某种原因,FineUploader使得blob存储URI错误。

这就是我在测试组件中实现FineUploader的方法:

import React, { Component } from 'react';

import FineUploaderAzure from 'fine-uploader-wrappers/azure'
import Gallery from './gallery/index';

const uploader = new FineUploaderAzure({
    options: {
        cors: {
            expected: true,
            sendCredentials: true
        },
        signature: {
            endpoint: 'https://myapp.com/api/getsas'
        },
        request: {
            endpoint: 'https://myaccount.blob.core.windows.net/test-container'
        },
        uploadSuccess: {
            endpoint: 'https://myapp.com/api/done'
        }
    }
})

class Index extends Component {
    render() {
        return (
            <Gallery uploader={uploader} />
        )
    }
}

export default Index;

这是我在控制台中看到的错误。看起来FineUploader使用错误的URI来存储blob。 How Classes are Found

知道可能导致这种情况的原因吗?

更新: 根据@ GauravMantri的建议,我在选项部分将endpoint更改为containerUrl,但这似乎也没有帮助。这是它的样子:

const uploader = new FineUploaderAzure({
    options: {
        cors: {
            expected: true,
            sendCredentials: true
        },
        signature: {
            endpoint: 'https://myapp.com/api/getsas'
        },
        request: {
            containerUrl: 'https://myaccount.blob.core.windows.net/test-container'
        },
        uploadSuccess: {
            endpoint: 'https://myapp.com/api/done'
        }
    }
})

这是我通过邮递员发送请求时得到的SAS: 我发送的请求是:

http://example.com/api/files/get/sas?blobUri=https://myaccount.blob.core.windows.net/test-container/test.txt&_method=put

这是我收到的SAS:

"?sv=2017-04-17&sr=b&sig=7pXTnI2r8uGyZms12T9cRvHg1XlLI53ZJtwPUwGElnY%3D&st=2017-12-28T14%3A02%3A56Z&se=2017-12-28T14%3A22%3A56Z&sp=w"

1 个答案:

答案 0 :(得分:1)

我能够让它发挥作用。基本上还有一些需要记住的事情:

  1. FineUploader配置中,您需要endpoint属性,并且该属性应包含您要上传的blob容器的网址。这就是我的代码中配置的样子:

    var uploader = new qq.azure.FineUploader({
        debug: true,
        element: document.getElementById("uploader"),
        cors: {
            expected: true,
            sendCredentials: false
        },
        signature: {
            endpoint: 'http://localhost:63194/users/sas'
        },
        request: {
            endpoint: 'https://account-name.blob.core.windows.net/container-name'
        },
    
    })
    
  2. 获取共享访问签名(SAS)的API应返回blob URL + SAS Token。 API的blobUrl参数应该是blob的绝对URL。这是我用于API的代码(请不要按原样使用它,因为下面的代码没有考虑_method参数):

    [Route("sas")]
    [HttpGet]
    public async Task<HttpResponseMessage> Sas(string blobUri)
    {
        var credentials = new StorageCredentials("account-name", "account-key");
        var blob = new CloudBlockBlob(new Uri(blobUri), credentials);
        var sasParameters = new SharedAccessBlobPolicy()
        {
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
            Permissions = SharedAccessBlobPermissions.Write
        };
        var sasToken = blob.GetSharedAccessSignature(sasParameters);
        var returnValue = blob.Uri.AbsoluteUri + sasToken;
        var resp = new HttpResponseMessage(HttpStatusCode.OK);
        resp.Content = new StringContent(returnValue, System.Text.Encoding.UTF8, "text/plain");
        return resp;
    }
    
  3. 我从这里下载了Fine Uploader Azure相关文件:https://fineuploader.com/customize.html并用它来创建一个简单的HTML页面来测试它。这是我的HTML页面的样子:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="fine-uploader-gallery.min.css" rel="stylesheet">
        <script src="azure.fine-uploader.min.js""></script>
        <script type="text/template" id="qq-template">
            <div class="qq-uploader-selector qq-uploader qq-gallery" qq-drop-area-text="Drop files here">
                <div class="qq-total-progress-bar-container-selector qq-total-progress-bar-container">
                    <div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-total-progress-bar-selector qq-progress-bar qq-total-progress-bar"></div>
                </div>
                <div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone>
                    <span class="qq-upload-drop-area-text-selector"></span>
                </div>
                <div class="qq-upload-button-selector qq-upload-button">
                    <div>Upload a file</div>
                </div>
                <span class="qq-drop-processing-selector qq-drop-processing">
                    <span>Processing dropped files...</span>
                    <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
                </span>
                <ul class="qq-upload-list-selector qq-upload-list" role="region" aria-live="polite" aria-relevant="additions removals">
                    <li>
                        <span role="status" class="qq-upload-status-text-selector qq-upload-status-text"></span>
                        <div class="qq-progress-bar-container-selector qq-progress-bar-container">
                            <div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-progress-bar-selector qq-progress-bar"></div>
                        </div>
                        <span class="qq-upload-spinner-selector qq-upload-spinner"></span>
                        <div class="qq-thumbnail-wrapper">
                            <img class="qq-thumbnail-selector" qq-max-size="120" qq-server-scale>
                        </div>
                        <button type="button" class="qq-upload-cancel-selector qq-upload-cancel">X</button>
                        <button type="button" class="qq-upload-retry-selector qq-upload-retry">
                            <span class="qq-btn qq-retry-icon" aria-label="Retry"></span>
                            Retry
                        </button>
    
                        <div class="qq-file-info">
                            <div class="qq-file-name">
                                <span class="qq-upload-file-selector qq-upload-file"></span>
                                <span class="qq-edit-filename-icon-selector qq-btn qq-edit-filename-icon" aria-label="Edit filename"></span>
                            </div>
                            <input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text">
                            <span class="qq-upload-size-selector qq-upload-size"></span>
                            <button type="button" class="qq-btn qq-upload-delete-selector qq-upload-delete">
                                <span class="qq-btn qq-delete-icon" aria-label="Delete"></span>
                            </button>
                            <button type="button" class="qq-btn qq-upload-pause-selector qq-upload-pause">
                                <span class="qq-btn qq-pause-icon" aria-label="Pause"></span>
                            </button>
                            <button type="button" class="qq-btn qq-upload-continue-selector qq-upload-continue">
                                <span class="qq-btn qq-continue-icon" aria-label="Continue"></span>
                            </button>
                        </div>
                    </li>
                </ul>
    
                <dialog class="qq-alert-dialog-selector">
                    <div class="qq-dialog-message-selector"></div>
                    <div class="qq-dialog-buttons">
                        <button type="button" class="qq-cancel-button-selector">Close</button>
                    </div>
                </dialog>
    
                <dialog class="qq-confirm-dialog-selector">
                    <div class="qq-dialog-message-selector"></div>
                    <div class="qq-dialog-buttons">
                        <button type="button" class="qq-cancel-button-selector">No</button>
                        <button type="button" class="qq-ok-button-selector">Yes</button>
                    </div>
                </dialog>
    
                <dialog class="qq-prompt-dialog-selector">
                    <div class="qq-dialog-message-selector"></div>
                    <input type="text">
                    <div class="qq-dialog-buttons">
                        <button type="button" class="qq-cancel-button-selector">Cancel</button>
                        <button type="button" class="qq-ok-button-selector">Ok</button>
                    </div>
                </dialog>
            </div>
        </script>
    
        <title>Fine Uploader Gallery UI</title>
    </head>
    <body>
        <div id="uploader"></div>
        <script>
            // Some options to pass to the uploader are discussed on the next page
            var uploader = new qq.azure.FineUploader({
                debug: true,
                element: document.getElementById("uploader"),
                cors: {
                    expected: true,
                    sendCredentials: false
                },
                signature: {
                    endpoint: 'http://localhost:63194/users/sas'
                },
                request: {
                    endpoint: 'https://account-name.blob.core.windows.net/container-name'
                },
    
            })
        </script>
    </body>
    </html> 
    

    运行此代码后,我能够毫无问题地在blob容器中上传文件。