Пытаетесь скачать файл из хранилища BLOB-объектов Azure?Javascript

Форум по Javascript
Ответить
Anonymous
 Пытаетесь скачать файл из хранилища BLOB-объектов Azure?

Сообщение Anonymous »

Я пытаюсь загрузить файлы из хранилища BLOB-объектов Azure через restful API.
У меня есть следующий фрагмент, который возвращает URL-адрес, аналогичный тому, который вы можете автоматически сгенерировать через их портал.< /p>
async fetchBlobUrl (containerName: string, blobId: string): Promise {
// Step 1: Prepare the canonical URL path for the blob
const urlPath = `/${containerName}/${blobId}`

// Step 2: Set SAS token parameters
const expiryDate = new Date(new Date().valueOf() + 3600 * 1000) // Expiry is 1 hour from now
const expiry = expiryDate.toISOString().slice(0, 19) + 'Z' // Format: 2025-01-16T14:33:00Z

const startDate = new Date() // Start time is now
const startTime = startDate.toISOString().slice(0, 19) + 'Z' // Format: 2025-01-16T13:33:00Z

const permissions = 'r' // Read permission
const sasVersion = '2022-11-02' // SAS API version
const protocol = 'https' // Protocol should be https
const resourceType = 'b' // 'b' for blob

// Step 3: Construct the canonical string to sign (fixing the structure)
const stringToSign = [
permissions, // permissions
startTime, // start time
expiry, // expiry
`/blob/${this.accountName}/${containerName}/${blobId}`, // the blob's URL path
'', // identifier (optional)
'', // IP (optional)
protocol, // protocol (https)
sasVersion, // SAS version
resourceType, // resource type ('b' for blob)
].join('\n')

// Step 4: Create a signature using your account key
const hash = crypto
.createHmac('sha256', Buffer.from(this.accountKey, 'base64'))
.update(stringToSign, 'utf8')
.digest('base64')

// Step 5: Generate the SAS token by appending the signature
const sasToken = `sp=${permissions}&st=${startTime}&se=${expiry}&spr=${protocol}&sv=${sasVersion}&sr=${resourceType}&sig=${encodeURIComponent(hash)}`

// Step 6: Construct the final blob URL
return Promise.resolve(`https://${this.accountName}.blob.core.windows.net${urlPath}?${sasToken}`)
}

Но каждый раз, когда я тестирую, я получаю ошибку:
Signature did not match. String to sign used was etc etc etc


Подробнее здесь: https://stackoverflow.com/questions/793 ... ob-storage
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»