简体   繁体   English

如何使用node.js将Blob移到Azure的子目录中

[英]How to move blob into subdirectory in Azure using node.js

I've got a node.js service that should donwload file from azure, process it and when its done- move it to a "done" subfolder within the same container. 我有一个node.js服务,该服务应从azure中下载文件,进行处理,并在完成后将其移动到同一容器中的“完成”子文件夹中。

I've been digging in the documentation for over an hour and couldn't find any method to copy a blob or rename it, because if I create it from the beginning as done/blob-name it will visually present it inside subfolder so renaming might also do the trick (I know thats how it works and thats fine with me, I filter the blobs by ignoring all those who contains done/ in their name). 我已经在文档中挖掘了一个多小时,并且找不到任何方法来复制Blob或对其进行重命名,因为如果我从头开始以done / blob-name创建它,它将在视觉上将其显示在子文件夹中,因此重命名也许也可以解决这个问题(我知道那是如何工作的,这对我来说还不错,我通过忽略所有包含完成名称的人来过滤这些斑点)。

The only thing close enough is the startCopyFromURL which is a member of the "blob" class, but I can't get how to use it- because I only provide a source but no destination... all I ask is for a simple function copyBlob(sourceBlobURL, dstBlobUrl) -.- 唯一足够接近的是startCopyFromURL,它是“ blob”类的成员,但我无法获得使用方法的信息-因为我仅提供源但没有目标...我只想提供一个简单的功能copyBlob(sourceBlobURL,dstBlobUrl)-.-

strangely enough the Azure portal does not give any copy / renaming options, but there is something called "storage explorer" (which is accessible through azure portal) and as far as I understood it- its a GUI enabling advanced operations on blobs, including copy operations and sub-folders management. 奇怪的是,Azure门户没有提供任何复制/重命名选项,但是据我所知,它有一个称为“存储资源管理器”的文件(可通过azure门户访问)-它的GUI可以对blob进行高级操作,包括复制操作和子文件夹管理。 So if there's a GUI to do that, there must be a programmatic way... 因此,如果有GUI可以做到这一点,那么必须有一种编程方式...

im using the (kinda) new v10 sdk ( here ), only requiring the @azure/storage-blob part in my script. 我使用的是(kinda)新的v10 sdk( 在此处 ),仅在脚本中需要@ azure / storage-blob部分。

Thanks a bunch for your help! 多谢您的协助!

UPDATE: I did find references to a method called "startCopyAsync", but all examples I could find online are from sdks to different languages (c# for example) and are 1-2 years old, while the new JS sdk is officialy the primary sdk for js only since few weeks ago (july '19). 更新:我确实找到了对称为“ startCopyAsync”的方法的引用,但是我可以在线找到的所有示例都是从sdk到不同的语言(例如c#),并且存在1-2年,而新的JS sdk正式是主要sdk仅在几周前(19年7月)开始使用js。 Couldn't find any mention of this function in the new JS sdk. 在新的JS sdk中找不到此功能的任何提及。

You cannot rename a blob; 您无法重命名Blob; you can only copy a blob to a new blob (or upload a new blob). 您只能将一个Blob复制到一个新Blob(或上传一个新Blob)。

Regarding subdirectories: there's no such thing. 关于子目录:没有这样的事情。 The hierarchy is: 层次结构是:

account / container / blob

What looks like a subdirectory, in a blob's name, is just path characters. 在Blob名称中,看起来像子目录的只是路径字符。 For example: 例如:

https://myblobs.blob.core.windows.net/mycontainer/images/image1.jpg

Is really a blob named "images/image1.jpg" 确实是一个名为"images/image1.jpg"的Blob

Various blob-listing calls allow you to filter using the separator character, giving the illusion of subdirectories. 各种Blob列表调用使您可以使用分隔符进行过滤,从而产生子目录的错觉。

So in your case, you would need to upload your "processed" blob to a new blob (whether in the same container or a new container). 因此,在您的情况下,您需要将“已处理”的Blob上传到新的Blob(无论是在同一容器中还是在新容器中)。 And if you upload to the same container, you need to come up with a new name. 而且,如果您上传到同一容器,则需要重新命名。 If you use a different container for your processed blobs, then you won't have to worry about coming up with a different blob name. 如果您为处理的Blob使用其他容器,则无需担心使用其他Blob名称。

You can use startCopyFromURL , as you yourself assumed. 您可以按照自己的假设使用startCopyFromURL The source is the first argument, and the target is the Blob object you use to call this method. 源是第一个参数,目标是用于调用此方法的Blob对象。 So, the flow is something like: 因此,流程类似于:

  1. Create a target Blob object (empty, including all sub-folders you need, like done ) 创建一个目标Blob对象(为空,包括您需要的所有子文件夹,如done
  2. Call startCopyFromURL on it providing the URL of the source 调用startCopyFromURL并提供源的URL
  3. Wait for promise resolution. 等待承诺解决。

A non-javascript example can be found here . 可以在此处找到非JavaScript示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM