简体   繁体   English

如何通过 PHP api 调用获取 AWS S3 存储桶位置?

[英]How to get the AWS S3 bucket location via PHP api call?

I am searching on the internet on how can I get the AWS s3 bucket region with an API call or directly in PHP using their library but have not luck finding the info.我正在互联网上搜索如何通过 API 调用或直接在 PHP 中使用他们的库获取 AWS s3 存储桶区域,但没有找到相关信息。

I have the following info available: Account credentials, bucket name, access key + secret.我有以下可用信息:帐户凭据、存储桶名称、访问密钥 + 机密。 That is for multiple buckets, that I have access to, and I need to get the region programatically, so logging in to aws console and checking out is not an option.这是针对多个存储桶,我可以访问,并且我需要以编程方式获取该区域,因此无法登录 aws 控制台并签出。

Assuming you have an instance of the AWS PHP Client in $client , you should be able to find the location with $client->getBucketLocation() .假设您在$client有一个 AWS PHP Client 实例,您应该能够使用$client->getBucketLocation()找到该位置。

Here is some example code:下面是一些示例代码:

<?php

$result = $client->getBucketLocation([
    'Bucket' => 'yourBucket',
]);

The result will look like this结果看起来像这样

[
    'LocationConstraint' => 'the-region-of-your-bucket',
]

When you create a S3 client, you can use any of the available regions in AWS, even if it's not one that you use.创建 S3 客户端时,您可以使用 AWS 中的任何可用区域,即使它不是您使用的区域。

$s3Client = new Aws\S3\S3MultiRegionClient([
            'version'     => 'latest',
            'region'      => 'us-east-1',
            'credentials' => [
                'key'    => $accessKey,
                'secret' => $secretKey,
            ],
        ]);

$region = $s3Client->determineBucketRegion($bucketname);

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

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