我打算将图像本地保存到 Dropbox Cloud,以便我可以获取要存储在数据库中的 URL,但问题是我看到 Dropbox 仅存储图像 URL,4 小时后它就会消失。所以我发现他们使用
createSharedLinkWithSettings
来获取永久链接 URL。
但它总是捕获错误 return 400 Bad Request,如下所示:
Spatie\Dropbox\Exceptions\BadRequest {#453
#message: ""
#code: 0
#file: "C:\xampp\htdocs\quanlybanhang\vendor\spatie\dropbox-api\src\Client.php"
#line: 658
+dropboxCode: null
+response: GuzzleHttp\Psr7\Response {#429
-reasonPhrase: "Bad Request"
-statusCode: 400
-headers: array:11 [
"Content-Type" => array:1 [
0 => "text/plain; charset=utf-8"
]
"Cache-Control" => array:1 [
0 => "no-cache"
]
"Content-Security-Policy" => array:1 [
0 => "sandbox allow-forms allow-scripts"
]
"X-Content-Type-Options" => array:1 [
0 => "nosniff"
]
"X-Dropbox-Request-Url" => array:1 [
0 => "/2/sharing/create_shared_link_with_settings"
]
"Date" => array:1 [
0 => "Sun, 05 May 2024 13:07:17 GMT"
]
"Server" => array:1 [
0 => "envoy"
]
"Content-Length" => array:1 [
0 => "284"
]
"Vary" => array:1 [
0 => "Accept-Encoding"
]
"X-Dropbox-Response-Origin" => array:1 [
0 => "far_remote"
]
"X-Dropbox-Request-Id" => array:1 [
0 => "c04575fab67542119e65e2702ed72711"
]
]
-headerNames: array:11 [
"content-type" => "Content-Type"
"cache-control" => "Cache-Control"
"content-security-policy" => "Content-Security-Policy"
"x-content-type-options" => "X-Content-Type-Options"
"x-dropbox-request-url" => "X-Dropbox-Request-Url"
"date" => "Date"
"server" => "Server"
"content-length" => "Content-Length"
"vary" => "Vary"
"x-dropbox-response-origin" => "X-Dropbox-Response-Origin"
"x-dropbox-request-id" => "X-Dropbox-Request-Id"
]
-protocol: "1.1"
-stream: GuzzleHttp\Psr7\Stream {#428
-stream: stream resource @597
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: 284
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
trace: {
C:\xampp\htdocs\quanlybanhang\vendor\spatie\dropbox-api\src\Client.php:658 {
Spatie\Dropbox\Client->determineException(ClientException $exception): Exception …
› if (in_array($exception->getResponse()->getStatusCode(), [400, 409])) {
› return new BadRequest($exception->getResponse());
› }
}
C:\xampp\htdocs\quanlybanhang\vendor\spatie\dropbox-api\src\Client.php:646 {
Spatie\Dropbox\Client->rpcEndpointRequest(string $endpoint, array $parameters = null, bool $isRefreshed = false): array …
› ) {
› throw $this->determineException($exception);
› }
arguments: {
$exception: GuzzleHttp\Exception\ClientException {#423 …}
}
}
C:\xampp\htdocs\quanlybanhang\vendor\spatie\dropbox-api\src\Client.php:141 {
Spatie\Dropbox\Client->createSharedLinkWithSettings(string $path, array $settings = []): array …
›
› return $this->rpcEndpointRequest('sharing/create_shared_link_with_settings', $parameters);
› }
arguments: {
$endpoint: "sharing/create_shared_link_with_settings"
$parameters: array:2 [ …2]
}
}
C:\xampp\htdocs\quanlybanhang\app\Admin\Controllers\ProductController.php:88 {
App\Admin\Controllers\ProductController->uploadImage($base64Image) …
› ];
› $link = $client->createSharedLinkWithSettings('/' . preg_replace('#^data:image/\w+;base64,#i', '', $base64Image), $settings);
› dd($link);
arguments: {
$path: "/iVBORw0KGgoAAAANSUhEUgAAAlgAAAJYCAMAAACJuGjuAAADAFBMVEUAAADj4t6xR1u5uLnDk468doC0Y2/azM7CvrfSzMfn4NDJyMfQy8Hq5dfk3MrPjZPFxMXCwsPBv8Dk5Odw ....
这是 POST 请求数据:
_token "lIJmrP9fuVyIukzhAa4zQZMh4pjOb9jQoSEqI60m"
product_name ""
product_desc ""
product_image "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAJYCAMAAACJuGjuAAADAFBMVEUAAADj4t6xR1u5uLnDk468doC0Y2/azM7CvrfSzMfn4NDJyMfQy8Hq5dfk3MrPjZPFxMXCwsPBv8Dk5OdwnFbMbXeet2jOY3P1793UlZ/IS2TFSmLISmLDRV3k3czj28ro4tDn383l38/r5dLLTGXi2cbo5NTm4tLs59TDSF/q48/m3cnz0Mfp4czt6dbHR1/l28fq59fh18LOxrjOSWK/RFvt6trLw7S4Qllnq0TJV2nQybzx69rUzL3Wz8Dd1sfLYG/Iv6/esabet63QT2jaqZ/NanbPdHzfv7PYoZrSh4rZ0sSuPlRnChXQfoPYrKXUkJDFj4Tjzr/hx7rxwrd6qkNruUbLmo+cMETVmJekN03wyr5uo0O+uayPLEHOk4fTn5Pc077Uybjm1MWKsEJuDR69iX+3OlHQpZysLkeLslmAL0O3s…NCkqBAbzM5wpjlXm65NT7fal/b45M8iCV76093sXPydDMl4Mj09zQLYiZLKvr+hDextaANgsVVr8OCNUL5hUZvc7unJ95cBW+VVwALbctS09Br5N/qZ8hPnRa/kqBH5pvFtuouIdWKfE8atCgqMKmI7653nPJPcgpUl+t0/hQpWi+9MzVCmsIv5UrtGtC990sA+opGjjEud8SC/i9n6BrexVfN70iJqvrtGaNXCS8gq8iXk9fI3d3k1QTscf3b//Qu4W0X/FpW8Jt++O//zn62X1q3WdR/2/7iLiAVUzHxxuZyJBFuZiNBNx/EeFV2cjI5qU3Ae/AE1EmsNxFpe1sVz5Dcgv82OnVTSph9z6KYQa7pBWNwYOud/glbt72E0uzHErT/7zJ3d4m645Xbgnp9+Qn/xf4va7v/xt7j7ttu7hVe/t0vHNAAAMAjAMv+md2CCkFZDk+uoMLUKAAAAAAAAAAAAAAAAtj1FK+Qc7cGDsgAAAABJRU5ErkJggg=="
category_id ""
product_condition "0"
product_content "Chưa+có+thông+tin+gì+cả."
publish "1"
product_status "1"
我使用product_image作为$base64Image参数传递到uploadImage函数中,这里是:
public function uploadImage($base64Image)
{
if (!isset($base64Image)) return null;
$imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image));
$filename = 'image_' . uniqid() . '.png';
$result = Storage::disk('dropbox')->put($filename, $imageData);
if ($result) {
$adapter = Storage::disk('dropbox')->getAdapter();
$client = $adapter->getClient();
$url = '/' . $filename;
$settings = [
'requested_visibility' => 'public',
];
$link = $client->createSharedLinkWithSettings('/' . preg_replace('#^data:image/\w+;base64,#i', '', $base64Image), $settings);
dd($link);
} else {
return $this->failure([], 'Somthing go wrong ways');
}
}