使用 PHP 查看 S3 中是否存在对象

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

Seeing if object exists in S3 using PHP

phpamazon-s3

提问by SSH This

I am using PHP and I am using the S3 API to upload a file, but I wanted to make sure that this exact filename doesn't already exist in the bucket before upload.

我正在使用 PHP 并使用 S3 API 上传文件,但我想确保在上传之前存储桶中不存在此确切文件名。

I have found a few examples online that use "file_get_contents" but doesn't this mean that you would have to download the entire file first? Usually, these files are about 10 mb, so ideally, I wouldn't really want to do this.

我在网上找到了一些使用“file_get_contents”的例子,但这不意味着你必须先下载整个文件吗?通常,这些文件大约为 10 mb,因此理想情况下,我真的不想这样做。

Is there perhaps a way to use "file_get_contents" without downloading the file?

有没有办法在不下载文件的情况下使用“file_get_contents”?

Or better yet, perhaps I could use an API request to see if the filename exists?

或者更好的是,也许我可以使用 API 请求来查看文件名是否存在?

It's not important to me whether or not the content, or filesize, is the same, just the filename.

内容或文件大小是否相同对我来说并不重要,只是文件名。

回答by waqas

Gets whether or not the specified Amazon S3 object exists in the specified bucket.

获取指定的存储桶中是否存在指定的 Amazon S3 对象。

AmazonS3 doesObjectExist

亚马逊S3 doesObjectExist

$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$response = $s3->doesObjectExist($bucket, 'test1.txt');

// Success? (Boolean, not a CFResponse object)
var_dump($response);

回答by Amir Md Amiruzzaman

try to use code below:

尝试使用以下代码:

$s3 = new S3();

$info = $s3->getObjectInfo($bucket, $filename);
if ($info)
{
    echo 'File exists';
}
else
{
    echo 'File does not exists';
}

download the S3 SDK from amazon for php. There is a class called S3; create an object of S3. The object will allow to call the getObjectInfo() method. Pass your S3 bucket name and the file name (often the file name is referred as key). The getObjectInfo() method will return some information if the file exists, otherwise the method will return FALSE.

从亚马逊为 php 下载 S3 SDK。有一个类叫S3;创建 S3 的对象。该对象将允许调用 getObjectInfo() 方法。传递您的 S3 存储桶名称和文件名(通常文件名称为键)。如果文件存在,getObjectInfo() 方法将返回一些信息,否则该方法将返回 FALSE。

回答by David

Please note that the other suggestions are based on version 1 of the AWS SDK for PHP. For version 2, you'll want to be familiar with the latest guide found here:

请注意,其他建议基于适用于 PHP 的 AWS 开发工具包的版本 1。对于第 2 版,您需要熟悉此处提供的最新指南:

http://docs.aws.amazon.com/aws-sdk-php/guide/latest/index.html

http://docs.aws.amazon.com/aws-sdk-php/guide/latest/index.html

The "Getting Started" section in the link above will help you get the SDK installed and setup, so be sure to take your time reading through those docs if you haven't done so already. When you're done with the setup, you'll want to be familiar with the stream wrapper method found here:

上面链接中的“入门”部分将帮助您安装和设置 SDK,因此如果您还没有阅读这些文档,请务必花时间阅读这些文档。完成设置后,您将需要熟悉此处的流包装器方法:

http://docs.aws.amazon.com/aws-sdk-php/guide/latest/feature-s3-stream-wrapper.html

http://docs.aws.amazon.com/aws-sdk-php/guide/latest/feature-s3-stream-wrapper.html

Finally, below is a brief, real-life example of how you could use it in the flow of your code.

最后,下面是一个简短的真实示例,说明如何在代码流中使用它。

require('vendor/autoload.php');

// your filename
$filename = 'my_file_01.jpg';
// this will use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from env vars
$s3 = Aws\S3\S3Client::factory();
// S3_BUCKET must also be defined in env vars
$bucket = getenv('S3_BUCKET')?: die('No "S3_BUCKET" config var in found in env!');
// register stream wrapper method
$s3->registerStreamWrapper();
// does file exist
$keyExists = file_exists("s3://".$bucket."/".$filename);
if ($keyExists) {
    echo 'File exists!';
}

回答by Paul

If you have or have the ability to install the PECL extension HTTPthen you can use http_headto make a head request easily and check whether the response was 200 or 404.

如果您有或有能力安装 PECL 扩展HTTP,那么您可以使用http_head轻松发出 head 请求并检查响应是 200 还是 404。