通过REST API在S3中上传图像

发布于 2025-02-11 21:30:42 字数 1560 浏览 1 评论 0原文

因此,我有一个HTML表单,该表单接受用户的图像。

<form enctype="multipart/form-data" method="POST" action="call-api.php">
  <input type="file" id="myFile" name="image" class="btn btn-default">
  <input type="submit" name="upload" class="btn btn-default">
</form>

然后将图像发送到AWS API网关,该网关使用S3作为代理,并将图像上传到S3中。我正在使用php curl来进行REST API调用:

<?php

if (isset($_FILES['image']['tmp_name'])) {
    //create a handler for curl function 
    $curl = curl_init(); //initialzie cURL session

    //The CURLFile class 
    $cfile = new CURLFile($_FILES['image']['tmp_name'], $_FILES['image']['type'], $_FILES['image']['name']);

    //use array to post data to different server or within localhost 
    $data = array("myimage" => $cfile);


    curl_setopt($curl, CURLOPT_URL, 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/v1/test57827/'.$_FILES['image']['name']);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    //assign  execute curl to a response variable
    $result = curl_exec($curl);
?>

此代码的问题是,它在S3中将文件上传到名称$ _ files ['image'] ['name'] ['name']但该文件不是图像是因为从s3下载该文件后,我无法在普通图像预览器程序中打开它,并获得错误“未识别文件格式”,

但是当我使用终端中的curl命令调用我的API时,则图像被上传到S3中,因为当我从S3下载它,可以在普通图像预览程序中打开它。

curl --request POST -H "Content-Type: */*"  --data-binary "@image.png"  https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/v1/test57827/myfile.jpeg

我尝试了多个版本的PHP代码,但是它们都无法以正确的方式上传图像。我不想使用AWS SDK。 PHP卷曲的任何指导都将不胜感激。

So I have a html form which accepts the image from user.

<form enctype="multipart/form-data" method="POST" action="call-api.php">
  <input type="file" id="myFile" name="image" class="btn btn-default">
  <input type="submit" name="upload" class="btn btn-default">
</form>

Then the image is sent to AWS API Gateway which uses S3 as proxy and uploads the image in S3. I am using PHP curl to make REST API call:

<?php

if (isset($_FILES['image']['tmp_name'])) {
    //create a handler for curl function 
    $curl = curl_init(); //initialzie cURL session

    //The CURLFile class 
    $cfile = new CURLFile($_FILES['image']['tmp_name'], $_FILES['image']['type'], $_FILES['image']['name']);

    //use array to post data to different server or within localhost 
    $data = array("myimage" => $cfile);


    curl_setopt($curl, CURLOPT_URL, 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/v1/test57827/'.$_FILES['image']['name']);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    //assign  execute curl to a response variable
    $result = curl_exec($curl);
?>

The problem with this code is that it uploads a file in S3 with name $_FILES['image']['name'] but that file is not an image because After downloading that file from S3, i cant open it in normal image previewer program and get error "file format is not recognised"

But when i use curl command from terminal to call my api then the image gets uploaded in S3 because when i download it from S3 , i can open it in normal image previewer program.

curl --request POST -H "Content-Type: */*"  --data-binary "@image.png"  https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/v1/test57827/myfile.jpeg

I have tried multiple versions of my php code but none of them is able to upload the image in correct way. I dont want to use AWS SDK. Any guidance with php curl will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2025-02-18 21:30:42

它未经测试,但是也许您需要一个内容类型来发布,以便API可以正确处理请求的数据:

curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: multipart/form-data' ]);

编辑:
我找到了一个显示工作的教程: https://ryansechrest.com/2012/07/send-and-and-receive-binary-files-using-php-and-curl/ 也许这就是要走的路。

it is untested, but maybe you need a content-Type for POST, so that the API can process the request's data correctly:

curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: multipart/form-data' ]);

EDIT:
I found a tutorial which show work: https://ryansechrest.com/2012/07/send-and-receive-binary-files-using-php-and-curl/ Maybe that's the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文