Azure put 阻止列表在 sw 中返回 400

问题描述 投票:0回答:1

我正在尝试使用 blob 存储,首先我使用 azure put block rest api 服务来写入数据,然后我尝试使用 put block list api 服务,但每次都会收到状态代码 400。

let baseURL = NSURL(string: Constants.AZURE_URL + containerType.rawValue + Constants.FOLDER_DIVIDER + fileName + token + "&comp=blocklist")! as URL

    var putBlockListRequest = URLRequest(url: baseURL)
    putBlockListRequest.httpMethod = "PUT"
    putBlockListRequest.setValue("2015-05-04", forHTTPHeaderField: "x-ms-version")
    let blockListXML = generateBlockListXML(blockIDs: [blockId])
    putBlockRequest.setValue(String(blockListXML.data(using: .utf8)!.count), forHTTPHeaderField: "Content-Length")
    putBlockRequest.httpBody = blockListXML.data(using: .utf8)

    let putBlocListTask = session.dataTask(with: putBlockListRequest) { (data: Data?, response: URLResponse?, error: Error?) -> Void in
        result = self.cloudConnectionHandler(error)
        semaphore.signal()
    }
    putBlocListTask.resume()
ios swift azure azure-blob-storage azure-storage
1个回答
0
投票

我使用 Azure Put Block REST API 服务写入数据,然后尝试使用 Put Block List API 服务,但每次都收到状态代码 400。

您可以参考此MS-Document来了解您的错误。如果该块无法位于已提交或未提交块的列表中,则它不会包含在 Blob 中,并且 Blob 存储将以状态代码 400 进行响应。

在我的环境中,我尝试使用 JavaScript 代码来执行这两个请求,例如

Put block
Put block list.
将文件上传到 Azure Blob 存储。

代码:

const request = require('request')
const fs = require('fs')
const _ = require('lodash')

const account = 'venkat123'
const containerName = 'test'
const blobName = 'venkat235.wav'

let i = 0
const blockIdArray = []
const sastoken = 'your-sas token'
const readStream = fs.createReadStream('sample.wav', { highWaterMark: 1024 * 1024 })

readStream.on('data', function (chunk) {
  i++
  const blobContent = chunk
  const contentLength = chunk.length
  const blockId = Buffer.from('block' + i).toString('base64')
  console.log(blockId)
  blockIdArray.push(blockId)
  const optionsPutBlock = {
    url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}?comp=block&blockid=${blockId}&${sastoken}`,
    headers: {
      'Content-Length': contentLength
    },
    body: blobContent
  }
  request.put(optionsPutBlock, callbackPutBlock)
})

readStream.on('end', () => {
  const xmlBlockList = '<?xml version="1.0" encoding="utf-8"?><BlockList>' + (_.map(blockIdArray, (id) => { return `<Latest>${id}</Latest>` })).join('') + '</BlockList>'
  console.log(xmlBlockList)
  const xmlBlockListLength = new TextEncoder().encode(xmlBlockList).length
  const optionsPutBlockList = {
    url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}?comp=blocklist&${sastoken}`,
    headers: {
      'Content-Length': xmlBlockListLength,
    },
    body: xmlBlockList
  }
  console.log(optionsPutBlockList.body)
  request.put(optionsPutBlockList, callbackPutBlockList)
})

function callbackPutBlock (error, response, body) {
  console.log(response.statusCode, response.statusMessage,"Block created")
  if (error) {
    console.log(error)
  }
}

function callbackPutBlockList (error, response, body) {
  console.log(response.statusCode, response.statusMessage)
  if (error) {
    console.log(error)
  }
}

输出:

Ym***sx
Ym**2sy
Ym***2sz
Ym***s0
<?xml version="1.0" encoding="utf-8"?><BlockList><Latest>Ym***sx</Latest><Latest>Ymx**sy</Latest><Latest>Ymx***sz</Latest><Latest>Ym**s0</Latest></BlockList>
<?xml version="1.0" encoding="utf-8"?><BlockList><Latest>Ym**2sx</Latest><Latest>Ymx**sy</Latest><Latest>Ymx**sz</Latest><Latest>Ymx**s0</Latest></BlockList>
201 Created
201 Created Block created
201 Created Block created
201 Created Block created
201 Created Block created

enter image description here

传送门: enter image description here

参考:

Azure Block Blob:尝试创建块并提交它们时“指定的块列表无效” - Stack Overflow,作者:Mayank Patel。

© www.soinside.com 2019 - 2024. All rights reserved.