Microsoft Azure:如何在 blob 容器中创建子目录

发布于 2024-08-28 15:45:24 字数 868 浏览 6 评论 0原文

如何在 Blob 容器中创建子目录

,例如

在我的 Blob 容器 http://veda .blob.core.windows.net/document/

如果我存储一些文件,它将是

http://veda.blob.core.windows.net/document/ 1.txt

http://veda.blob.core.windows.net/document/ 2.txt

现在,如何创建子目录

http://veda.blob.core.windows.net/document/folder /

这样我就可以存储文件

http://veda.blob.core.windows.net/文档/文件夹/1.txt

How to create a sub directory in a blob container

for example,

in my blob container http://veda.blob.core.windows.net/document/

If I store some files it will be

http://veda.blob.core.windows.net/document/1.txt

http://veda.blob.core.windows.net/document/2.txt

Now, how to create a sub directory

http://veda.blob.core.windows.net/document/folder/

So that I can store files

http://veda.blob.core.windows.net/document/folder/1.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

仄言 2024-09-04 15:45:25

尝试 Azure 示例时遇到类似问题 第一个无服务器应用
这是我如何通过删除 $web 前面的 \ 来解决的信息。

注意:启用静态网站时,$web 容器会自动创建。
从未在任何地方见过 $root 容器。

//getting Invalid URI error while following tutorial as-is
az storage blob upload-batch -s . -d \$web --account-name firststgaccount01

//Remove "\" @destination param
az storage blob upload-batch -s . -d $web --account-name firststgaccount01

Got similar issue while trying Azure Sample first-serverless-app.
Here is the info of how i resolved by removing \ at front of $web.

Note: $web container was created automatically while enable static website.
Never seen $root container anywhere.

//getting Invalid URI error while following tutorial as-is
az storage blob upload-batch -s . -d \$web --account-name firststgaccount01

//Remove "\" @destination param
az storage blob upload-batch -s . -d $web --account-name firststgaccount01
前事休说 2024-09-04 15:45:25

我需要从 Jenkins 管道执行此操作,因此需要将文件上传到特定文件夹名称,而不是上传到根容器文件夹。我使用 --destination-path 可以是 folderfolder1/folder2

az storage blob upload-batch --account-name $AZURE_STORAGE_ACCOUNT --destination ${CONTAINER_NAME} --destination-path ${VERSION_FOLDER} --source ${BUILD_FOLDER} --account-key $ACCESS_KEY

希望这对某人有帮助

I needed to do this from Jenkins pipeline, so, needed to upload files to specific folder name but not to the root container folder. I use --destination-path that can be folder or folder1/folder2

az storage blob upload-batch --account-name $AZURE_STORAGE_ACCOUNT --destination ${CONTAINER_NAME} --destination-path ${VERSION_FOLDER} --source ${BUILD_FOLDER} --account-key $ACCESS_KEY

hope this help to someone

两人的回忆 2024-09-04 15:45:25

没有直接选项来创建文件夹/目录。但是,如果您希望将某些内容上传到文件夹,那么在上传文件时,您需要在高级部分下传递文件夹名称。例如,如果我想将图像上传到名为文件夹的资产,那么我的上传窗口将如下所示。enter此处图片描述

这将创建一个名为 asset 的文件夹,并将文件上传到该文件夹​​。需要注意的是文件夹名称和文件名称区分大小写。

There is no direct option to create a folder/directory. But if you wish to upload something to folder then while uploading file you need to pass folder name under advance section. For Example If I want to upload a image to an asset named folder then my upload window will look like this.enter image description here

This will create a folder names asset and will upload file to that folder. And point to be noted is folder name and file name are case sensitive.

薄情伤 2024-09-04 15:45:25

正如 @Egon 提到的,

如果您通过 azure 门户上传:

单击上传 >高级>上传到文件夹,例如 dir1/dir2/dir3

检查以下内容:https://i.sstatic.net/Aq5OQ。 .png

As @Egon mentioned,

If you are uploading through azure portal:

click upload > advanced > Upload to folder e.g dir1/dir2/dir3

check this: https://i.sstatic.net/Aq5OQ.png

罪歌 2024-09-04 15:45:25

C#< /a> 而不是上面接受的英语:

CloudBlobContainer container = new CloudBlobContainer(new Uri(sasUri));
CloudBlockBlob blob = container.GetBlockBlobReference(filePathInSyncFolder);
LocalFileSysAccess.LocalFileSys uploadfromFile = new LocalFileSysAccess.LocalFileSys();
uploadfromFile.uploadfromFilesystem(blob, localFilePath, eventType);

在我看来,在 Node.JS 上的 CoffeeScript 中更简单:

blobService.createBlockBlobFromText 'containerName', (path + '$$.$
), '', (err, result)->
    if err
        console.log 'failed to create path', err
    else
        console.log 'created path', path, result

C# instead of accepted English above:

CloudBlobContainer container = new CloudBlobContainer(new Uri(sasUri));
CloudBlockBlob blob = container.GetBlockBlobReference(filePathInSyncFolder);
LocalFileSysAccess.LocalFileSys uploadfromFile = new LocalFileSysAccess.LocalFileSys();
uploadfromFile.uploadfromFilesystem(blob, localFilePath, eventType);

In my opinion, it's simpler in CoffeeScript on Node.JS:

blobService.createBlockBlobFromText 'containerName', (path + '$$.$
), '', (err, result)->
    if err
        console.log 'failed to create path', err
    else
        console.log 'created path', path, result
久隐师 2024-09-04 15:45:24

要添加 Egon 所说的内容,只需创建名为“folder/1.txt”的 blob,它就会起作用。无需创建目录。

To add on to what Egon said, simply create your blob called "folder/1.txt", and it will work. No need to create a directory.

乖乖哒 2024-09-04 15:45:24

实际上只有一层容器。您实际上可以创建一个“文件系统”,例如分层存储,但实际上所有内容都将位于一层,即它所在的容器中。

为了创建像存储这样的虚拟“文件系统”,您可以使用包含“/”的 blob 名称,以便您可以按照自己的存储方式执行任何操作。此外,最棒的是,您可以通过给出部分字符串(最多为“/”)来在虚拟级别搜索 blob。

这两件事,向路径添加“/”和用于搜索的部分字符串,一起创建虚拟“文件系统”存储。

There is actually only a single layer of containers. You can virtually create a "file-system" like layered storage, but in reality everything will be in 1 layer, the container in which it is.

For creating a virtual "file-system" like storage, you can have blob names that contain a '/' so that you can do whatever you like with the way you store. Also, the great thing is that you can search for a blob at a virtual level, by giving a partial string, up to a '/'.

These 2 things, adding a '/' to a path and a partial string for search, together create a virtual "file-system" storage.

饭团 2024-09-04 15:45:24

在 Azure 门户中,我们在上传文件时有以下选项:

在此处输入图像描述

In Azure Portal we have below option while uploading file :

enter image description here

—━☆沉默づ 2024-09-04 15:45:24

@afr0 有一条评论询问如何过滤文件夹。

有两种方法使用 GetDirectoryReference 或循环遍历容器 blob 并检查类型。下面的代码是用 C# 编写的,

CloudBlobContainer container = blobClient.GetContainerReference("photos");

//Method 1. grab a folder reference directly from the container
CloudBlobDirectory folder = container.GetDirectoryReference("directoryName");

//Method 2. Loop over container and grab folders.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
    if (item.GetType() == typeof(CloudBlobDirectory))
    {
        // we know this is a sub directory now
        CloudBlobDirectory subFolder = (CloudBlobDirectory)item;

        Console.WriteLine("Directory: {0}", subFolder.Uri);
    }
}

请阅读此内容以获取更深入的介绍: http://www.codeproject.com/Articles/297052/Azure-Storage-Blobs-Service-Working-with-Directori

There is a comment by @afr0 asking how to filter on folders..

There is two ways using the GetDirectoryReference or looping through a containers blobs and checking the type. The code below is in C#

CloudBlobContainer container = blobClient.GetContainerReference("photos");

//Method 1. grab a folder reference directly from the container
CloudBlobDirectory folder = container.GetDirectoryReference("directoryName");

//Method 2. Loop over container and grab folders.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
    if (item.GetType() == typeof(CloudBlobDirectory))
    {
        // we know this is a sub directory now
        CloudBlobDirectory subFolder = (CloudBlobDirectory)item;

        Console.WriteLine("Directory: {0}", subFolder.Uri);
    }
}

read this for more in depth coverage: http://www.codeproject.com/Articles/297052/Azure-Storage-Blobs-Service-Working-with-Directori

无言温柔 2024-09-04 15:45:24

如果您使用 Microsoft Azure Storage Explorer,则有一个“新建文件夹”按钮,可让您在容器中创建文件夹。
这实际上是一个虚拟文件夹:

在此处输入图像描述

If you use Microsoft Azure Storage Explorer, there is a "New Folder" button that allows you to create a folder in a container.
This is actually a virtual folder:

enter image description here

几度春秋 2024-09-04 15:45:24

您不需要创建子目录。只需创建 blob 容器 并使用变量 filename 等文件名,如下代码所示:

string filename = "document/tech/user-guide.pdf";
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(filename);
blob.StreamWriteSizeInBytes = 20 * 1024;
blob.UploadFromStream(fileStream); // fileStream is System.IO.Stream

You do not need to create sub directory. Just create blob container and use file name like the variable filename as below code:

string filename = "document/tech/user-guide.pdf";
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(filename);
blob.StreamWriteSizeInBytes = 20 * 1024;
blob.UploadFromStream(fileStream); // fileStream is System.IO.Stream
无敌元气妹 2024-09-04 15:45:24

对于那些在动态目录中苦苦挣扎的人
根据版本 12,

<PackageReference Include="Azure.Storage.Blobs" Version="12.10.0"/>

您可以轻松地用反斜杠分隔目录或文件夹路径。
在这种情况下,它们将自动创建。
示例:

public async Task UploadFile(string env, string filePath, string filename, Guid companyId, Guid assetId, string baseKey)
{
    var blobContainer = blobServiceClient.GetBlobContainerClient("graphs-data");
    if (!blobContainer.Exists())
    {
      blobContainer.Create();
    }
    var blobClient = blobContainer.GetBlobClient($"input/{env}/{companyId}/iotasset/{assetId}/{baseKey}/{filename}");
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
      await blobClient.UploadAsync(fs, overwrite: true);
}

Azure 存储资源管理器中的结果视图
四个文本文件已上传

For someone struggling with dynamic directories
As per Version 12

<PackageReference Include="Azure.Storage.Blobs" Version="12.10.0"/>

You can easily have the directory or folder paths separated by a backslash.
They will be created automatically in this case.
Example:

public async Task UploadFile(string env, string filePath, string filename, Guid companyId, Guid assetId, string baseKey)
{
    var blobContainer = blobServiceClient.GetBlobContainerClient("graphs-data");
    if (!blobContainer.Exists())
    {
      blobContainer.Create();
    }
    var blobClient = blobContainer.GetBlobClient(
quot;input/{env}/{companyId}/iotasset/{assetId}/{baseKey}/{filename}");
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
      await blobClient.UploadAsync(fs, overwrite: true);
}

The results views in Azure storage explorer
The four text files uploaded

吃兔兔 2024-09-04 15:45:24

也想添加 UI 门户 方式来做。
如果您想创建文件夹结构,则必须使用每个文件的完整路径来完成。

输入图片此处描述

您需要单击“上传 Blob”,展开“高级”并将其放置在“上传到文件夹”的路径中。

假设您有一个要上传的文件夹资产,该文件夹的内容如下所示下面

js 文件夹下有一个名为 main.js 的文件夹,则需要在上传到文件夹中输入路径“assets/js”。
现在必须对每个文件执行此操作。如果您有很多文件,建议您以编程方式执行此操作。

Wanted to add the UI portal Way to do as well.
In case you want to create the folder structure, you would have to do it with the complete path for each file.

enter image description here

You need to Click on Upload Blob, Expand the Advanced and put it the path saying "Upload to Folder"

So Lets say you have a folder assets you want to upload and the content of the folder look like below

enter image description here

And if you have a folder under js folder with name main.js, you need to type in the path "assets/js" in upload to folder.
Now This has to be done for each file. In case you have a lot of file, its recommended you do it programmatically.

提赋 2024-09-04 15:45:24

正如@Egon 上面提到的,BLOB 存储中没有真正的文件夹管理。

您可以使用文件名中的“/”来实现文件系统的某些功能,但这有很多限制(例如,如果您需要重命名“文件夹”会发生什么?)。

作为一般规则,我会将文件尽可能平坦放在容器中,并让我的应用程序管理我想要向最终用户公开的任何结构(例如管理嵌套文件夹结构 >在我的数据库中,为每个文件都有一条记录,使用容器名和文件名引用 BLOB)。

As @Egon mentioned above, there is no real folder management in BLOB storage.

You can achieve some features of a file system using '/' in the file name, but this has many limitations (for example, what happen if you need to rename a "folder"?).

As a general rule, I would keep my files as flat as possible in a container, and have my application manage whatever structure I want to expose to the end users (for example manage a nested folder structure in my database, have a record for each file, referencing the BLOB using container-name and file-name).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文