![](/img/trans.png)
[英]Executing AWS CLI command from php results in Unable to locate credentials
[英]unable to locate aws credential file from php
在下面的aws cli s3復制命令的PHP文件中,它無法正常工作,給出的錯誤憑據未找到。
但是,當它在UNIX命令行中執行時,它可以正常工作。
在主目錄中擁有aws憑證,並授予其他用戶讀取權限
~/.aws
total 8
-rw------- 1 root root 10 Aug 31 14:47 config
-rw-r--r-- 1 root root 349 Aug 31 14:50 credentials
exec("aws s3 cp $s3_location $local_dest")
使用php時,您可能應該使用php sdk。
您可以在此處查看獲取證書的不同最佳方法
示例-如果您的credentials
文件類似
[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY
並且您的config
文件包含如下區域
[default]
region=us-east-1
您可以創建一個簡單的S3客戶端
<?php
use Aws\S3\S3Client;
// Instantiate a client with the credentials from the project1 profile
$s3Client = S3Client::factory();
如果您沒有完整的配置設置或想要更改區域,例如
// Instantiate a client with the credentials from the project1 profile
$s3Client = S3Client::factory(array(
'region' => 'us-west-2',
));
要復制對象,可以查看copy-object方法
use Aws\S3\S3Client;
$sourceBucket = '*** Your Source Bucket Name ***';
$sourceKeyname = '*** Your Source Object Key ***';
$targetBucket = '*** Your Target Bucket Name ***';
$targetKeyname = '*** Your Target Key Name ***';
// Instantiate the client.
$s3 = S3Client::factory();
// Copy an object.
$s3->copyObject(array(
'Bucket' => $targetBucket,
'Key' => $targetKeyname,
'CopySource' => "{$sourceBucket}/{$sourceKeyname}",
));
注意:您不需要授予對憑據文件中其他文件的訪問權限,請將其保留為
~/.aws
total 8
-rw------- 1 root root 10 Aug 31 14:47 config
-rw------- 1 root root 349 Aug 31 14:50 credentials
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.