Amazon S3 boto - ¿cómo crear una carpeta?


¿Cómo puedo crear una carpeta bajo un bucket usando la biblioteca boto para Amazon s3?

Seguí el manual, y creé las claves con permiso, metadatos, etc., pero no hay dónde en la documentación del boto se describe cómo crear carpetas bajo un bucket, o crear una carpeta bajo carpetas en bucket.

Author: Eric Leschinski, 2009-12-21

9 answers

No hay ningún concepto de carpetas o directorios en S3. Puede crear nombres de archivo como "abc/xys/uvw/123.jpg", que muchas herramientas de acceso S3 como S3Fox muestran como una estructura de directorios, pero en realidad es un solo archivo en un cubo.

 105
Author: user240469,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-20 10:33:48

Supongamos que desea crear la carpeta abc / 123 / en su cubo, es pan comido con Boto

k = bucket.new_key('abc/123/')
k.set_contents_from_string('')

O utilice la consola

 35
Author: TomNg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-24 02:33:41

Con AWS SDK. Net funciona perfectamente, simplemente agregue " / " al final de la cadena de nombre de la carpeta:

var folderKey =  folderName + "/"; //end the folder name with "/"
AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey);
var request = new PutObjectRequest();
request.WithBucketName(AWSBucket);
request.WithKey(folderKey);
request.WithContentBody(string.Empty);
S3Response response = client.PutObject(request);

Luego actualice su consola AWS y verá la carpeta

 17
Author: elranu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-16 16:56:58

Añada "_folder folder folder" al nombre de su carpeta y llame a put.

    String extension = "_$folder$";
    s3.putObject("MyBucket", "MyFolder"+ extension, new ByteArrayInputStream(new byte[0]), null);

Véase: http://www.snowgiraffe.com/tech/147/creating-folders-programmatically-with-amazon-s3s-api-putting-babies-in-buckets/

 8
Author: Mamad Asgari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-24 12:27:24

S3 no tiene una estructura de carpetas, pero hay algo llamado claves.

Podemos crear /2013/11/xyz.xls y se mostrarán como carpetas en la consola. Pero la parte de almacenamiento de S3 tomará eso como el nombre del archivo.

Incluso al recuperar observamos que podemos ver archivos en una carpeta particular (o claves) usando el método ListObjects y usando el parámetro Prefix.

 1
Author: Balaram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-15 23:31:26

Es muy fácil crear carpetas. En realidad es sólo la creación de claves.

Puedes ver mi siguiente código estaba creando una carpeta con utc_time como nombre.

Do remember termina la clave con '/' como a continuación, esto indica que es una clave:

Key = 'folder1 /' + utc_time + '/'

client = boto3.client('s3')
utc_timestamp = time.time()


def lambda_handler(event, context):

    UTC_FORMAT = '%Y%m%d'
    utc_time = datetime.datetime.utcfromtimestamp(utc_timestamp)
    utc_time = utc_time.strftime(UTC_FORMAT)
    print 'start to create folder for => ' + utc_time

    putResponse = client.put_object(Bucket='mybucketName',
                                    Key='folder1/' + utc_time + '/')

    print putResponse
 1
Author: Gabriel Wu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-07 07:13:59

Aunque puede crear una carpeta añadiendo "/" a su folder_name. Bajo el capó, S3 mantiene la estructura plana a diferencia de su NFS regular.

var params = {
            Bucket : bucketName,
            Key : folderName + "/"
        };
s3.putObject(params, function (err, data) {});
 1
Author: vpage,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-20 16:39:07

Usa esto:

import boto3
s3 = boto3.client('s3')
bucket_name = "YOUR-BUCKET-NAME"
directory_name = "DIRECTORY/THAT/YOU/WANT/TO/CREATE" #it's name of your folders
s3.put_object(Bucket=bucket_name, Key=(directory_name+'/'))
 1
Author: Reza Amya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-14 20:17:23

Aparentemente ahora puede crear carpetas en S3. No estoy seguro desde cuándo, pero tengo un cubo en la zona" Estándar " y puedo elegir Crear carpeta desde el menú desplegable de Acción.

 0
Author: Perelx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-21 09:44:04