dapr / dapr/components-contrib
alicloud oss can't upload image
- Dominant language
- Go
- Stars
- 602
- Forks
- 580
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 6
Description
**evnrionment**
machine: mac M1
docker image: daprio/daprd:edge, build by docker-compose
component: bindings.alicloud.oss
**problem**
when i use alicloud oss binding, i can upload a file with text, follow is my code:
```PHP
$resp = $this->client->post($this->daprSideCardUrl . '/v1.0/bindings/bingdingAliyunoss', [
RequestOptions::JSON => ['data' => 'hello, world', "metadata" => ["key" => "1.txt"], 'operation' => 'create']
]);
```
i didn't use the php sdk, just use guzzlehttp to call the sidecar api, and it worked. I login aliyun,and i can see the file is there, i download the file, open it, and yes, it contains the string "hello, world".
however, if i want to upload a image file, i don't know what's the value of `data` field, i have tried different ways, but none worked.
- set the base64 data of image to the `data` field
like "data:image/png;base64,xxxxxxxx", it won't throw exception. i login aliyun, i can see the file, but it's broken, it is not a image file. i think it means the image data is not correct. follow is my code.
```PHP
$file = BASE_PATH . '/assets/1.jpeg';
$imageInfo = getimagesize($file);
$imageBase64Data = "data:{$imageInfo['mime']};base64," . chunk_split(base64_encode(file_get_contents($file)));
$resp = $this->client->post($this->daprSideCardUrl . '/v1.0/bindings/bingdingAliyunoss', [
RequestOptions::JSON => ['data' => $imageBase64Data, "metadata" => ["key" => "1.jpeg"], 'operation' => 'create']
]);
```
- set the base64 data of image without "data:image/png;base64," prefix to the `data` field
then, i delete the prefix "data:image/png;base64," of "data:image/png;base64,xxxxxxxx", the result is the same as above
- use `file_get_contents` to get the content of image, and set to `data` field
```PHP
$file = BASE_PATH . '/assets/1.jpeg';
$resp = $this->client->post($this->daprSideCardUrl . '/v1.0/bindings/bingdingAliyunoss', [
RequestOptions::JSON => ['data' => file_get_contents($file), "metadata" => ["key" => "1.jpeg"], 'operation' => 'create']
]);
```
it would throw an exception because that makes the payload a wrong json format.
Contributor guide
Assessment
This issue has not been assessed yet.