我遇到与Executing AWS CLI command from php results in Unable to locate credentials
完全相同的问题我做了:
cd ~/.aws
mv config credentials
但它没有帮助。
所以我所做的是,配置和凭据都具有相同的内容:
[默认]
aws_access_key_id = AKIAJIPLT472C32RD6AQ
aws_secret_access_key = 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
output = json
region = ap-southeast-1
我在PHP文件中使用的代码是:
$s3 = system("aws s3 cp /var/www/html/v2/upload/__rmx4hqf.png s3://<bucket>/somefolder 2>&1");
答案 0 :(得分:0)
我的猜测是php
命令无法找到aws凭据文件,因为您运行的用户$ aws configure
与php的有效用户不同。您没有提供有关如何运行configure命令或php脚本执行方式的足够信息,所以这只是我的猜测。
如果您通过环境变量AWS_CONFIG_FILE
明确传递凭证文件的路径会发生什么?(例如$s3 = system("AWS_CONFIG_FILE=/path/to/credential aws s3 cp ...
)?
答案 1 :(得分:0)
尝试添加您的区域
$s3 = system("aws s3 cp /var/www/html/v2/upload/__rmx4hqf.png s3://<bucket>/somefolder 2>&1 --region ap-southeast-1");
答案 2 :(得分:0)
// inclusion of libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
Reads a string allocated by the stream.
It stops at newline, not included in string.
Returns NULL to EOF
*/
char *my_getline(FILE *stream) { // statement of function
char *line = NULL; // this is just the pointer initialization
size_t pos = 0; // definition of position variables and init
int c; // a variable to store the temporary character
while ((c = getc(stream)) != EOF) // read every character until the end of the file
{
char *newp = realloc(line, pos + 2); // To dynamically allocate memory, with reference to the number of characters and more '2' is only to compensate for the null character and the character (since it is 0)
if (newp == NULL) { // checks whether memory has been properly associated or not.
free(line); // if the line is not free the blank
return NULL; // interrupts the program and returns NULL
}
line = newp; // if memory is allocated correctly stores the memory allocated to the line pointer
if (c == '\n') // if a new line is detected
break; // interrupts the while cycle
line[pos++] = (char)c; // stores the character in dynamic memory and the new character in the new location.
}
if (line) { // if the line contains something then a null character is added at the end to complete that string.
line[pos] = '\0';
}
return line; // returns the contents of the line.
}
int main(void) { // main statement
char *str, *sub; // character punctuation statement
size_t len1, len2, i, count = 0; // unsigned value statement "size_t is equal to unsigned int" so may also be <0
int pos[count]; // declare a count array to insert the index then print it in reverse
int j;
// Here is the main string
printf("Enter Main String: \n"); // print the entry and enter the main string
str = my_getline(stdin); // inserts the entered string inside the pointer using my_getline function and using getchar analogue stdin to make the entered characters input from the standard input
// here is the substring to look for
printf("Enter substring to search: \ n"); // print the entry and enter the main substring
sub = my_getline(stdin); // inserts the entered string inside the pointer using my_getline function and using getchar analogue stdin to make the entered characters input from the standard input
if (str && sub) { // if string and substring && = and
len1 = strlen(str); // inserts the string length in the len1 variable
len2 = strlen(sub); // inserts the length of the string in the len2 variable
for (i = 0; i + len2 <= len1; i++) { // loop for with the control that the substring is less than or equal to the main string ie len2 <= len1
if (! memcmp(str + i, sub, len2)) { // here uses the memcmp function to compare the string and substring byte bytes
count++; // count variable that is incremented each time the sub is found in p
// here is where it gets in output
// If the substring was found mold the index with the locations it was found
pos[count] = i + 1;
printf( "%d\n", pos[count]);
}
}
// print to get reverse output
printf("number of times%d", count);
// print to get reverse output
printf("%d", count);
for (j = count - 1; j >= 0; j--)
printf("%d", pos[j]);
if (count == 0) { // if count is = 0 ie the substring was not found string string not found
// otherwise if not found
printf("Subtry not found \n");
}
}
// free releases the memory area that was reserved for the string and substrings so that it can be reused in the next run
free(str);
free(sub);
return 0; // exit analog
}
查看apache用户ENV变量
要将变量添加到apache2 ENV,您需要使用putenv(“string”);
你需要以这种方式添加aws cerdentials
getenv();
putenv('AWS_DEFAULT_REGION=' . $region);
putenv('AWS_ACCESS_KEY_ID=' . $key);
putenv('AWS_SECRET_ACCESS_KEY=' . $secret);e
当我尝试从php文件
运行aws cli时,只有这个解决方案适合我