[英]Microsoft Azure: How to create sub directory in a blob container
How to create a sub directory in a blob container如何在 blob 容器中创建子目录
for example,例如,
in my blob container http://veda.blob.core.windows.net/document/在我的 blob 容器中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/1.txt
http://veda.blob.core.windows.net/document/2.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/
http://veda.blob.core.windows.net/document/folder/
So that I can store files这样我就可以存储文件
http://veda.blob.core.windows.net/document/folder/1.txt
http://veda.blob.core.windows.net/document/folder/1.txt
To add on to what Egon said, simply create your blob called "folder/1.txt", and it will work.要补充 Egon 所说的内容,只需创建名为“folder/1.txt”的 blob,它就会起作用。 No need to create a directory.
无需创建目录。
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.
您可以虚拟地创建类似于分层存储的“文件系统”,但实际上一切都将在 1 层中,即它所在的容器。
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.为了创建像存储这样的虚拟“文件系统”,您可以使用包含“/”的 blob 名称,以便您可以按照您的存储方式执行任何您喜欢的操作。 Also, the great thing is that you can search for a blob at a virtual level, by giving a partial string, up to a '/'.
此外,最棒的是您可以在虚拟级别搜索 blob,方法是提供部分字符串,最多为“/”。
These 2 things, adding a '/' to a path and a partial string for search, together create a virtual "file-system" storage.这两件事,将“/”添加到路径和用于搜索的部分字符串,共同创建了一个虚拟的“文件系统”存储。
There is a comment by @afr0 asking how to filter on folders.. @afr0 有一条评论询问如何过滤文件夹..
There is two ways using the GetDirectoryReference
or looping through a containers blobs and checking the type.有两种方法可以使用
GetDirectoryReference
或循环遍历容器 blob 并检查类型。 The code below is in C#下面的代码是在 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阅读本文以获得更深入的报道: http : //www.codeproject.com/Articles/297052/Azure-Storage-Blobs-Service-Working-with-Directori
If you use Microsoft Azure Storage Explorer , there is a "New Folder" button that allows you to create a folder in a container.如果您使用Microsoft Azure 存储资源管理器,则有一个“新建文件夹”按钮可让您在容器中创建文件夹。 This is actually a virtual folder:
这实际上是一个虚拟文件夹:
You do not need to create sub directory.您不需要创建子目录。 Just create blob container and use file name like the variable filename as below code:
只需创建 blob 容器并使用文件名,如变量文件名,如下代码:
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
For someone struggling with dynamic directories对于那些在动态目录中苦苦挣扎的人
As per Version 12根据版本 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($"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 Azure 存储资源管理器中的结果视图
As @Egon mentioned above, there is no real folder management in BLOB storage.正如@Egon 上面提到的,BLOB 存储中没有真正的文件夹管理。
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).作为一般规则,我会将我的文件尽可能地保持在容器中,并让我的应用程序管理我想要向最终用户公开的任何结构(例如管理我的数据库中的嵌套文件夹结构,为每个文件,使用容器名和文件名引用 BLOB)。
Wanted to add the UI portal Way to do as well.想添加 UI 门户方式也可以。 In case you want to create the folder structure, you would have to do it with the complete path for each file.
如果要创建文件夹结构,则必须使用每个文件的完整路径。
You need to Click on Upload Blob, Expand the Advanced and put it the path saying "Upload to Folder"您需要单击“上传 Blob”,展开“高级”并将其放在“上传到文件夹”的路径中
So Lets say you have a folder assets you want to upload and the content of the folder look like below假设您有一个要上传的文件夹资产,文件夹的内容如下所示
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.如果你在js文件夹下有一个名为main.js的文件夹,你需要在上传到文件夹中输入路径“assets/js”。 Now This has to be done for each file.
现在这必须为每个文件完成。 In case you have a lot of file, its recommended you do it programmatically.
如果您有很多文件,建议您以编程方式进行。
Got similar issue while trying Azure Sample first-serverless-app .尝试 Azure Sample first-serverless-app 时遇到类似问题。
Here is the info of how i resolved by removing \\ at front of $web.这是我如何通过删除 $web 前面的 \\ 来解决的信息。
Note: $web container was created automatically while enable static website.注意:$web 容器是在启用静态网站时自动创建的。 Never seen $root container anywhere.
从未在任何地方见过 $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
I needed to do this from Jenkins pipeline, so, needed to upload files to specific folder name but not to the root container folder.我需要从 Jenkins 管道执行此操作,因此,需要将文件上传到特定文件夹名称而不是根容器文件夹。 I use --destination-path that can be folder or folder1/folder2
我使用 --destination-path 可以是文件夹或文件夹 1 /文件夹 2
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希望这对某人有帮助
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.
需要注意的是文件夹名和文件名是区分大小写的。
Check out video demonstration of this feature using Python: https://youtu.be/Bnrargw-eM0使用 Python 查看此功能的视频演示: https://youtu.be/Bnrargw-eM0
C# instead of accepted English above: C#而不是上面接受的英文:
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:在我看来,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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.