PutObject 到目录 Amazon s3 / PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24665062/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:26:10  来源:igfitidea点击:

PutObject into directory Amazon s3 / PHP

phpamazon-web-servicesamazon-s3

提问by Fabrizio Fenoglio

I need to upload my files inside specific directories that I created on my amazon s3 storage. I always uploaded the files on the "absolute path" of my bucket doing something like so:

我需要将我的文件上传到我在亚马逊 s3 存储上创建的特定目录中。我总是将文件上传到我的存储桶的“绝对路径”上,执行如下操作:

$s3->putObject(array(
            'Bucket' => $bucket,
            'ContentType'   => $mime,
            'Key'           => $localImage,
            'ACL'           => 'public-read',
            'SourceFile'    => $localImage,
            'CacheControl'  => 'max-age=172800',
            "Expires"       => gmdate("D, d M Y H:i:s T", strtotime("+5 years")),
            'Metadata'      => array(
                'profile' => $localImage,
            ),
        )); 

How can I define where this file should be uploaded on a given directory?

如何定义此文件应上传到给定目录的位置?

回答by Jeremy Lindblom

You must include that information in the "Key" parameter. S3 isn't actually a filesystem, it's more like a big (hash table) associative array. The "Bucket" is the name of the hash table, and the "Key" is the key (e.g., $bucket[$key] = $content). So all path/directory information must be a part of the "Key".

您必须在“密钥”参数中包含该信息。S3 实际上不是一个文件系统,它更像是一个大的(哈希表)关联数组。“Bucket”是哈希表的名称,“Key”是键(例如,$bucket[$key] = $content)。所以所有的路径/目录信息都必须是“Key”的一部分。

$localImage = '/Users/jim/Photos/summer-vacation/DP00342654.jpg';
$s3->putObject(array(
    'Bucket'     => 'my-uniquely-named-bucket',
    'SourceFile' => $localImage,
    'Key'        => 'photos/summer/' . basename($localImage)
));

回答by Brian Sanchez

thank you Jeremy Lindblom, this is my python example that worked for me.

谢谢 Jeremy Lindblom,这是我的 Python 示例,对我有用。

import boto3
s3 = boto3.resource('s3')
data = open('/home/briansanchez/www/red-hat.jpg', 'rb')
s3.Bucket('briansanchez').put_object(Key='www/red-hat.jpg', Body=data)

回答by Pawan Nagar

Updated code according to the latest SDK of AWS:-

根据 AWS 的最新 SDK 更新代码:-

$result = $s3->putObject(array(
    'Bucket' => 'bucket name of S3',
    'Key' => 'pawan-trying',
    'SourceFile' => 'local image path or document root image path ',
    'ContentType' => 'image',
    'ACL' => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY',
    'Metadata' => array(
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
        ));