SharedKeyCredential不是构造函数. Azure存储反应应用程序 [英] SharedKeyCredential is not a constructor. Azure-storage react app

查看:130
本文介绍了SharedKeyCredential不是构造函数. Azure存储反应应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在React应用程序中将Blockblob上传到Azure-Storage.但是,出现以下错误.

I'm trying to use the following code to upload a blockblob to Azure-Storage in a react app. However, I'm getting the following error.

TypeError:SharedKeyCredential不是构造函数

TypeError: SharedKeyCredential is not a constructor

有什么想法吗?

@ azure/storage-blob @ 10.3.0

@azure/storage-blob@10.3.0

import React from 'react';

const {
  Aborter,
  BlobURL,
  BlockBlobURL,
  ContainerURL,
  ServiceURL,
  StorageURL,
  SharedKeyCredential,
  AnonymousCredential,
  TokenCredential
} = require("@azure/storage-blob"); // Change to "@azure/storage-blob" in your package

function App() {
  return (
    <div className="App">

      <button onClick={onClicked()} />

    </div>
  );

  async function onClicked() {

    // Enter your storage account name and shared key
    const account = "REMOVED_MY_ACCOUNT";
    const accountKey = "REMOVED_ACCOUNT_KEY";

    // Use SharedKeyCredential with storage account and account key
    const sharedKeyCredential = new SharedKeyCredential(account, accountKey);

    // Use TokenCredential with OAuth token
    const tokenCredential = new TokenCredential("token");
    tokenCredential.token = "renewedToken"; // Renew the token by updating token field of token credential

    // Use AnonymousCredential when url already includes a SAS signature
    const anonymousCredential = new AnonymousCredential();

    // Use sharedKeyCredential, tokenCredential or anonymousCredential to create a pipeline
    const pipeline = StorageURL.newPipeline(sharedKeyCredential);

    // List containers
    const serviceURL = new ServiceURL(
      // When using AnonymousCredential, following url should include a valid SAS or support public access
      `https://${account}.blob.core.windows.net`,
      pipeline
    );

    // Create a container
    const containerName = `newcontainer${new Date().getTime()}`;
    const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);

    const createContainerResponse = await containerURL.create(Aborter.none);
    console.log(
      `Create container ${containerName} successfully`,
      createContainerResponse.requestId
    );

    // Create a blob
    const content = "hello";
    const blobName = "newblob" + new Date().getTime();
    const blobURL = BlobURL.fromContainerURL(containerURL, blobName);
    const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
    const uploadBlobResponse = await blockBlobURL.upload(
      Aborter.none,
      content,
      content.length
    );
    console.log(
      `Upload block blob ${blobName} successfully`,
      uploadBlobResponse.requestId
    );

  }
}

export default App;

我在调用错误的API.您可以创建一个使用.Net/React模板的新Visual Studio项目.这是我想要的代码示例.

I was calling the wrong API. You can create a new Visual Studio project that uses the .Net/React template. This was the code example I was looking for.

推荐答案

我是存储JS SDK的开发人员. SharedKeyCredential仅在Node.js运行时中可用.对于浏览器,出于安全考虑,请使用共享访问签名(SAS)或OAuth令牌进行身份验证.

I'm the developer of storage JS SDK. SharedKeyCredential is only available in Node.js runtime. For browsers, for security concerns, please use Shared Access Signature (SAS) or OAuth Token for your authentications.

这篇关于SharedKeyCredential不是构造函数. Azure存储反应应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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