如何使用适用于 Node.js 的 AWS 开发工具包在 s3 上创建文件夹或密钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19459893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create folder or key on s3 using AWS SDK for Node.js?
提问by jarmod
I'm using AWS SDK for Node.js to create a folder or key on s3. I searched on google, but I got nothing. Does anybody know how can I create a folder under my bucket with AWS SDK for Node.js? and how can you check if this folder exists in your bucket already?
我正在使用适用于 Node.js 的 AWS 开发工具包在 s3 上创建文件夹或密钥。我在谷歌上搜索,但我一无所获。有人知道如何使用适用于 Node.js 的 AWS 开发工具包在我的存储桶下创建文件夹吗?以及如何检查此文件夹是否已存在于您的存储桶中?
if you use console.aws.amazon.com, you can create a folder in your bucket easily. it seems I didn't figure it out how to create it with AWS SDK for Node.js?
如果您使用console.aws.amazon.com,您可以轻松地在您的存储桶中创建一个文件夹。似乎我不知道如何使用适用于 Node.js 的 AWS SDK 创建它?
回答by jarmod
S3 is not your typical file system. It's an object store. It has buckets and objects. Buckets are used to store objects, and objects comprise data (basically a file) and metadata (information about the file). When compared to a traditional file system, it's more natural to think of an S3 bucket as a drive rather than as a folder.
S3 不是典型的文件系统。这是一个对象存储。它有桶和对象。桶用于存储对象,对象包括数据(基本上是一个文件)和元数据(关于文件的信息)。与传统文件系统相比,将 S3 存储桶视为驱动器而不是文件夹更为自然。
You don't need to pre-create a folder structure in an S3 bucket. You can simply put an object with the key cars/ford/focus.pngeven if cars/ford/does not exist.
您无需在 S3 存储桶中预先创建文件夹结构。cars/ford/focus.png即使cars/ford/不存在,您也可以简单地放置带有键的对象。
It's valuable to understand what happens at the API level in this case:
在这种情况下,了解 API 级别发生的事情很有价值:
the putObjectcall will create an object at
cars/ford/focus.pngbut it will notcreate anything representing the intermediate folder structure ofcars/orcars/ford/.the actual folder structure does not exist, but is implied through
delimiter=/when you call listObjects, returning folders inCommonPrefixesand files inContents.you will not be able to test for the ford sub-folder using headObjectbecause
cars/ford/does not actually exist (it is not an object). Instead you have 2 options to see if it (logically) exists:- call
listObjectswith prefix=cars/ford/and find it inContents - call
listObjectswith prefix=cars/, delimiter=/and find it inCommonPrefixes
- call
该putObject调用将建立在一个对象
cars/ford/focus.png但它将不创建任何表示的中间文件夹结构cars/或cars/ford/。实际的文件夹结构不存在,但通过
delimiter=/调用listObjects时隐含的,返回文件夹 inCommonPrefixes和Contents.您将无法使用headObject测试 ford 子文件夹,因为
cars/ford/它实际上不存在(它不是一个对象)。相反,您有 2 个选项来查看它是否(逻辑上)存在:listObjects使用 prefix=调用cars/ford/并在其中找到它ContentslistObjects使用 prefix=cars/, delimiter=调用/并在CommonPrefixes
It is possible to create an S3 object that represents a folder, if you really want to. The AWS S3 console does this, for example. To create myfolder in a bucket named mybucket, you can issue a putObject call with bucket=mybucket, key=myfolder/, and size 0. Note the trailing forward slash.
如果您真的想要,可以创建一个代表文件夹的 S3 对象。例如,AWS S3 控制台执行此操作。要在名为 mybucket 的存储桶中创建 myfolder,您可以使用 bucket=mybucket、key=myfolder/ 和大小 0 发出 putObject 调用。注意尾部的正斜杠。
Here's an example of creating a folder-like object using the awscli:
以下是使用 awscli 创建文件夹类对象的示例:
aws s3api put-object --bucket mybucket --key cars/ --content-length 0
In this case:
在这种情况下:
the folder is actually a zero-sized object whose key ends in /. Note that if you leave off the trailing / then you will get a zero-sized object that appears to be a file rather than a folder.
you are now able to test for the presence of myfolder/subfolder/ in mybucket by issuing a headObject call with bucket=mybucket and key=myfolder/subfolder/.
该文件夹实际上是一个大小为零的对象,其键以 / 结尾。请注意,如果您不使用尾随的 / 那么您将得到一个零大小的对象,该对象看起来是一个文件而不是一个文件夹。
您现在可以通过发出带有 bucket=mybucket 和 key=myfolder/subfolder/ 的 headObject 调用来测试 mybucket 中是否存在 myfolder/subfolder/。
Finally, note that your folder delimiter can be anything you like, for example +, because it is simply part of the key and is not actually a folder separator (there are no folders). You can vary your folder delimiter from listObjects call to call if you like.
最后,请注意,您的文件夹分隔符可以是您喜欢的任何内容,例如 +,因为它只是键的一部分,实际上并不是文件夹分隔符(没有文件夹)。如果您愿意,您可以将文件夹分隔符从 listObjects 调用更改为调用。
回答by Liviu Costea
The code from @user2837831 doesn't seem to work anymore, probably with the new version of javascript sdk. So I am adding here the version of code that I am using to create a folder inside a bucket using node.js. This works with the 2.1.31 sdk. What is important is the '/' at the end of the Key value in params - using that it thinks you are trying to create a folder and not a file.
来自@user2837831 的代码似乎不再起作用,可能是新版本的 javascript sdk。所以我在这里添加了我用来使用 node.js 在存储桶中创建文件夹的代码版本。这适用于 2.1.31 sdk。重要的是 params 中 Key 值末尾的“/” - 使用它认为您正在尝试创建文件夹而不是文件。
var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var s3Client = new AWS.S3();
var params = { Bucket: 'your_bucket_goes_here', Key: 'folderInBucket/', ACL: 'public-read', Body:'body does not matter' };
s3Client.upload(params, function (err, data) {
if (err) {
console.log("Error creating the folder: ", err);
} else {
console.log("Successfully created a folder on S3");
}
});
回答by James Skinner
A folder in a bucket is just another bucket. So you can use headBucketto check if it exists and create it with createBucketif it doesn't. Something like this:
存储桶中的文件夹只是另一个存储桶。因此,您可以使用headBucket它来检查它是否存在,如果不存在则使用它来创建它createBucket。像这样的东西:
var AWS = require('aws-sdk'),
s3 = new AWS.S3(),
bucketFolder = 'bucketA/folderInBucketA';
s3.headBucket({Bucket:bucketFolder},function(err,data){
if(err){
s3.createBucket({Bucket:bucketFolder},function(err,data){
if(err){ throw err; }
console.log("Bucket created");
});
} else {
console.log("Bucket exists and we have access");
}
});
==== Update 2017-02-22 ====
==== 更新 2017-02-22 ====
As pointed out in the comment, this is miss-leading. In 2013 I guess it did result in a "folder" being created (as far as the S3 UI is concerned).
正如评论中指出的那样,这是一种误导。在 2013 年,我猜它确实导致创建了一个“文件夹”(就 S3 UI 而言)。
If you run the above with the current AWS SDK, it will create an empty object at key "folderInBucketA" in bucket "bucketA". I don't think that is useful to anyone so please disregard this answer.
如果您使用当前的 AWS 开发工具包运行上述内容,它将在存储桶“bucketA”中的键“folderInBucketA”处创建一个空对象。我认为这对任何人都没有用,所以请忽略这个答案。
回答by Nat
I find that we do not need an explicit directory creation call anymore.
我发现我们不再需要显式的目录创建调用了。
Just the following works for me and automatically creates a directory hierarchy as I need.
以下仅适用于我并根据需要自动创建目录层次结构。
var userFolder = 'your_bucket_name' + '/' + variable-with-dir-1-name + '/' + variable-with-dir-2-name;
// IMPORTANT : No trailing '/' at the end of the last directory name
AWS.config.region = 'us-east-1';
AWS.config.update({
accessKeyId: 'YOUR_KEY_HERE',
secretAccessKey: 'your_secret_access_key_here'
});
var bucket = new AWS.S3({
params: {
Bucket: userFolder
}
});
var contentToPost = {
Key: <<your_filename_here>>,
Body: <<your_file_here>>,
ContentEncoding: 'base64',
ContentType: <<your_file_content_type>>,
ServerSideEncryption: 'AES256'
};
bucket.putObject(contentToPost, function (error, data) {
if (error) {
console.log("Error in posting Content [" + error + "]");
return false;
} /* end if error */
else {
console.log("Successfully posted Content");
} /* end else error */
})
.on('httpUploadProgress',function (progress) {
// Log Progress Information
console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
});
回答by ADITYA KUMAR
In console, the link generated first would be the bucket created path and second would be the folder structure.
在控制台中,首先生成的链接是存储桶创建的路径,其次是文件夹结构。
var AWS = require("aws-sdk");
var path = require('path')
// Set the region
AWS.config.update({
region: "us-east-2",
accessKeyId: "your aws acces id ",
secretAccessKey: "your secret access key"
});
s3 = new AWS.S3();
var bucketParams = {
Bucket: "imageurrllll",
ACL: "public-read"
};
s3.createBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Location);
var folder_name = 'root_folder'
//this is for local folder data path
var filePath = "./public/stylesheets/user.png"
//var child_folder='child'
var date = Date.now()
var imgData = `${folder_name}_${date}/` +
path.basename(filePath);
var params = {
Bucket: 'imageurrllll',
Body: '', //here you can give image data url from your local directory
Key: imgData,
ACL: 'public-read'
};
//in this section we are creating the folder structre
s3.upload(params, async function(err, aws_uploaded_url) {
//handle error
if (err) {
console.log("Error", err);
}
//success
else {
console.log("Data Uploaded in:", aws_uploaded_url.Location)
}
})
}
});

