简体   繁体   English

Php - 亚马逊s3如何检查我的连接是否成功

[英]Php - Amazon s3 how do I check my connection is success or not

I am using amazon-php-sdk. 我正在使用amazon-php-sdk。 In my application I'm accepting key and secret value from a form and passing for connecting to Aws. 在我的应用程序中,我接受表单中的密钥和秘密值,并通过连接到Aws。 Here is my code. 这是我的代码。

<?php
require 'aws-autoloader.php';
use Aws\S3\S3Client;
try {
    $s3Client = S3Client::factory(array(
        'key' => 'my key',
        'secret' => 'my secret key'
    ));
}
catch(S3Exception $e) {

   echo 'Msg' . $e->getMessage();
} 
?>
  1. How do I check whether the connection is success or not ? 如何检查连接是否成功?
  2. How can I check the already created object using the passed key so I that I don't want to create the object again. 如何使用传递的密钥检查已创建的对象,以便我不想再次创建对象。 I need to create the object only if the passed key is different from the already created one. 我只有在传递的键与已创建的键不同时才需要创建对象。

As Michael pointed out, you have to make an actual request to check the connection.AWS discourages any unnecessary validation requests which will affect the performance of your application and your usage bill. 正如Michael指出的那样,您必须提出检查连接的实际请求.AWS不鼓励任何不必要的验证请求,这些请求会影响您的应用程序和使用账单的性能。

But if you need to validate you can use ListBucket or HEAD request on a bucket. 但是如果需要验证,可以在存储桶上使用ListBucketHEAD请求。

Checkout this best practises article . 查看此最佳实践文章

require 'vendor/autoload.php';     
use Aws\S3\S3Client;

try {

// Instantiate the S3 client with your AWS credentials
$s3Client = S3Client::factory(array(
    'credentials' => array(
        'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
    )
));

$buckets = $s3Client->listBuckets();


}
catch(Exception $e) {

   exit($e->getMessage());
} 

To answer your second question.You can use doesObjectExist function which returns a boolean . 回答你的第二个问题。你可以使用返回布尔值的 doesObjectExist函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM