doujiong2533 2015-03-31 06:02
浏览 196
已采纳

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

I am using amazon-php-sdk. In my application I'm accepting key and secret value from a form and passing for connecting to 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.
  • 写回答

1条回答 默认 最新

  • doupao2277 2015-03-31 11:42
    关注

    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.

    But if you need to validate you can use ListBucket or HEAD request on a bucket.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)