我正在尝试将通过 HTML 表单提供的图像加载到 Dynamo Db 表中。
我正在对图像加载部分进行硬编码,但稍后我想取消注释:
$con=file_get_contents( basename($_FILES["fileToUpload"]["name"]) );
并将任何图像上传到 DynamoDB。
这是 html 文件:
<form action="upload-download-dynamodb.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
这是 upload-download-dynamodb.php
<?php
// Convert image to binary
//$con=file_get_contents( basename($_FILES["fileToUpload"]["name"]) );
$con=file_get_contents("Tuan.jpg");
$en=base64_encode($con);
$mime='image/gif';
$binary_data='data:' . $mime . ';base64,' . $en ;
?>
<img src="<?php echo $binary_data; ?>" alt="Test">
<?php
// Connect to Dynamodb
require './awssdkphp/aws-autoloader.php';
use Aws\DynamoDb\DynamoDbClient;
$client = DynamoDbClient::factory(array(
'profile' => 'default',
'region' => 'us-east-1' // replace with your desired region
));
// Put item inside a table
$responsePut = $client->putItem(array(
'TableName' => 'saleItems',
'Item' => array(
'itemID' => array('N' => 11 ), // Primary
'picture' => array('B' => openssl_random_pseudo_bytes(5)),
'array' => array('S' => $en),
'description' => array ('S' => "picture description" )
)
));
?>
如果我在终端中运行“upload-download-dynamodb.php”代码,它就可以正常工作(即 php upload-download-dynamodb.php)。如果我在浏览器(Firefox)上执行“upload-download-dynamodb.php”,则会显示图像,但数据不会上传到 Dynamo DB 中的“saleItems”表中。
有什么想法可以解释为什么“upload-download-dynamodb.php”代码在我通过终端执行它时将图片加载到 Dynamo DB 中,而不是在浏览器中执行它时(单击刷新按钮)?
顺便说一句,我授予了 upload-download-dynamodb.html 、upload-download-dynamodb.php 和 Tuan.jpg 的完全权限。
最佳答案
好吧,我终于明白发生了什么了。
我的凭据作为终端中的环境变量。由于某种原因,当我通过 html 文件执行 upload-download-dynamodb.php 时,PHP 无法访问这些凭据。
我通过添加以下代码行将凭据硬编码到 upload-download-dynamodb.php 文件:
use Aws\Common\Credentials\Credentials;
$credentials = new Credentials('YOUR_ACCESS_KEY','YOUR_SECRET_KEY');
$client = DynamoDbClient::factory(array(
'profile' => 'default',
'region' => 'us-east-1', // replace with your desired region
'credentials' => $credentials
));
现在代码可以正常工作了。
关于php - 使用 : PHP, Ubuntu、EC2 Amazon Web Services 将图像加载到 DynamoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29339032/