【问题标题】:Azure BlobStorage: 400 (One of the request inputs is out of range.)Azure Blob 存储:400(其中一个请求输入超出范围。)
【发布时间】:2018-04-10 09:42:51
【问题描述】:

当我尝试将 Blob 上传到我的 Azure 存储帐户时,我收到以下错误响应

<?xml version="1.0" encoding="utf-8"?>
  <Error>
    <Code>OutOfRangeInput</Code>
    <Message>One of the request inputs is out of range. 
           RequestId:--------------------------
           Time:2017-10-29T07:13:37.4218874Z
    </Message>
  </Error>

我正在上传多个 Blob,其中一些已成功上传,而另一些则没有。引发错误的那些具有大的 blob 名称(大约 100 个字符),因此假设它可能是由于 blob 名称的大小。但根据https://blogs.msdn.microsoft.com/jmstall/2014/06/12/azure-storage-naming-rules/,最大 blob 名称可以是 1024,而我的 blob 名称远低于该限制。

示例 blob 名称为“65/36/aluminium_03_group67_product_02pCube1_product_02group2_product_02Flow000_Albedo.png”

编辑代码以上传 Blob。

要上传的代码是 Javascript。我将文件分成多个块并上传。这里是负责上传文件的函数

function AzureFileUpload(file, uploadUrl, successCallback, progressCallback, errorCallback){
    this.file = file;
    this.uploadUrl = uploadUrl;
    this.successCallback = successCallback;
    this.progressCallback = progressCallback;
    this.errorCallback = errorCallback;
    this.reader = new FileReader();
    this.maxBlockSize = 256 * 1024;
    this.blockIds = [];
    this.totalBytesRemaining = this.file.size;
    this.currentFilePointer = 0;
    this.bytesUploaded = 0;
    this.uploadFlag = true;
    var self = this;
    this.reader.onloadend = function(evt) {
        if (evt.target.readyState == FileReader.DONE) { // DONE == 2
            var uri = self.uploadUrl + '&comp=block&blockid=' + self.blockIds[self.blockIds.length - 1];
            var requestData = new Uint8Array(evt.target.result);
            self.ReadBlock();
            if(self.uploadFlag){
                self.UploadBlock(requestData, uri);
            }
        }
    };
    this.ReadBlock();
}

AzureFileUpload.prototype.UploadBlock = function(requestData, blockUrl){
    var self = this;
    $.ajax({
        url: blockUrl,
        type: "PUT",
        data: requestData,
        processData: false,
        beforeSend: function(xhr) {
            xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob');
            xhr.setRequestHeader('x-ms-blob-cache-control', "public, max-age=864000");
        },
        success: function(data, status) {
            self.UpdateProgress(requestData.length);
            self.bytesUploaded += requestData.length;
            if (parseFloat(self.bytesUploaded) == parseFloat(self.file.size)) {
                self.CommitBlocks();
            }
        },
        error: function(xhr, desc, err) {
            // console.log(desc);
            // console.log(err);
            self.Error("Unexpected error occured while uploading model. Plaese try after some time");
        }
    });
};

AzureFileUpload.prototype.pad = function(number, length){
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
};

AzureFileUpload.prototype.ReadBlock = function(){
    if (this.totalBytesRemaining > 0) {
        var fileContent = this.file.slice(this.currentFilePointer, this.currentFilePointer + this.maxBlockSize);
        var blockId = "block-" + this.file.name + "-" + this.pad(this.blockIds.length, 6);
        this.blockIds.push(btoa(blockId));
        this.reader.readAsArrayBuffer(fileContent);
        this.currentFilePointer += this.maxBlockSize;
        this.totalBytesRemaining -= this.maxBlockSize;
        if (this.totalBytesRemaining < this.maxBlockSize) {
            this.maxBlockSize = this.totalBytesRemaining;
        }
    }
};

AzureFileUpload.prototype.UpdateProgress = function(bytesUploaded){
    console.log("Progress",bytesUploaded);
    if(this.progressCallback){
        this.progressCallback(bytesUploaded);
    }
};

AzureFileUpload.prototype.CommitBlocks = function(){
    var self = this;
    var uri = this.uploadUrl + '&comp=blocklist';
    var request = '<?xml version="1.0" encoding="utf-8"?><BlockList>';
    for (var i = 0; i < this.blockIds.length; i++) {
        request += '<Latest>' + this.blockIds[i] + '</Latest>';
    }
    request += '</BlockList>';
    $.ajax({
        url: uri,
        type: "PUT",
        data: request,
        beforeSend: function(xhr) {
            xhr.setRequestHeader('x-ms-blob-content-type', self.file.type);
            xhr.setRequestHeader('x-ms-blob-cache-control', "public, max-age=864000");
        },
        success: function(data, status) {
            console.log("Block Commited", data);
            if(self.successCallback){
                self.successCallback();
            }
        },
        error: function(xhr, desc, err) {
            self.Error("Unexpected error occured while uploading model. Plaese try after some time");
        }
    });
};

AzureFileUpload.prototype.Error = function(msg){
    this.CancelUpload();
    if(this.errorCallback){
        this.errorCallback(msg);
    }
};

AzureFileUpload.prototype.CancelUpload = function(){
    this.uploadFlag = false;
};

【问题讨论】:

  • 请分享上传blob的代码。
  • @GauravMantri 编辑了问题以包含代码。

标签: azure azure-blob-storage


【解决方案1】:

如果您使用连接字符串,这也可能是一个问题,请仔细检查它(和大小写),因为容器名称等区分大小写。你可以在这里阅读更多关于命名规则https://docs.microsoft.com/en-us/rest/api/storageservices/Naming-and-Referencing-Containers--Blobs--and-Metadata?redirectedfrom=MSDN

【讨论】:

    【解决方案2】:

    问题出在以下代码行:

    var blockId = "block-" + this.file.name + "-" + this.pad(this.blockIds.length, 6);
    

    基本上,块 ID 的最大长度可以是 64 个字节(参考:https://docs.microsoft.com/en-us/rest/api/storageservices/put-block - 请参阅 URI 参数部分)。因为您在块 id 计算中包含文件名并且您的文件名很大,所以您超出了此限制。

    请尝试以下代码行,您应该不会收到此错误:

    var blockId = "block-" + this.pad(this.blockIds.length, 6);
    

    请注意,块 id 的范围是一个 blob,因此您实际上不需要包含 blob 名称来使块 id 对 blob 是唯一的。

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 2015-07-24
      • 2014-12-17
      • 1970-01-01
      • 2018-01-30
      • 2021-11-15
      • 2021-07-10
      • 1970-01-01
      • 2019-03-21
      相关资源
      最近更新 更多