簡體   English   中英

AWS PHP SDK v3 檢查文件是否已存在於 S3 存儲桶中

[英]AWS PHP SDK v3 check if a file exists already in S3 bucket

我正在使用 PHP 和 AWS SDK v3 處理一個項目,我必須通過傳遞文件名來檢查文件是否已經存在。

這是我嘗試過的:

HTML 模板:

<html>
    <form id="form" action="check_existing.php" method="post">
        <input type="text" name="fName" >
        <input type="submit" name="submit" value="Submit"><br />
    </form>
</html>

來自check_existing.php:

include 'create_client.php';
if(isset($_POST["submit"])){            
    $filename = $_POST['fName'];
    $info = $s3->doesObjectExist($bucketName, $filename);
    print($info);
    if ($info)
    {
        echo 'File exists';
    }
    else
    {
    echo 'File does not exists';
    }
}

這是我創建s3客戶端的方式:

$s3 = new Aws\S3\S3Client([
    'region' => $region,
    'version' => 'latest',
    'credentials' => [
        'key'    => $IAM_KEY,
        'secret' => $IAM_SECRET,
    ],
]);

問題:它總是返回File does not exists

$s3 = new S3($s3_accesskey, $s3_secretkey); //create s3 object

$info = $s3->getObjectInfo($s3_bucket, $filename); // $filename can be path of file in bucket
if ($info){
    echo 'File exists';
}else{
    echo 'File does not exists';
}

getObjectInfo() 返回信息如下

Array
(
[date] => 1596690179
[time] => 1596651169
[hash] => 1234c1234341a7f2565c108b23b4aaca
[type] => image/jpeg
[size] => 18968
)

你可以試試這個。 參考: 在此處輸入鏈接描述

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

$test = $s3->doesObjectExist($bucket, 'testfile.jpg');

// Success or not? (Boolean, not a CFResponse object)
var_dump($test); // it will return boolean

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM