如何使用php将图像上传到数字海洋空间



我正在使用Spaces-API-master库,使用core php将图像上传到数字海洋空间。但我得到了错误:

SpacesAPIException:{"error":{"message":null,"code":null、"type":null和"http_code":null}}

我使用的库来自:github.com/SociallyDev/Spaces-API.

这是我的代码

require_once("vendor/autoload.php");  
$key = "xxxx"; 
$secret = "xxxxxx"; 
$space_name = "xxxx"; 
$region = "sgp1"; 
$space = new SpacesConnect($key, $secret, $space_name, $region); 
$path_to_file = "image.png"; 
$space->UploadFile($path_to_file, "public");

如何修复此错误?

我在尝试递归上传图像时遇到了这个问题,并且需要在空间中创建额外的目录。这是一种简单的php方法。

# if you need to display all errors if there is any
error_reporting(E_ALL);
ini_set('display_errors', 1);
// include the classes  PATH_TO_LIB defined in your config    
require(PATH_TO_LIB . 'aws/autoloader.php');
require(PATH_TO_LIB . 'spaces.php');
        
$key = "YOUR KEY";
$secret = "YOUR_SECRETE";
$space_name = "YOUR_SPACE_NAME";
$region = "YOUR_REGION";
// Initialise the space
$space = new SpacesConnect($key, $secret, $space_name, $region);
// I am expecting the relative path of the file to be upload
$source = "SOME_FOLDER/image.JPG";
$filename_on_space = "image.JPG";
$space->UploadFile($source, "public","path/to/directory/" . $filename_on_space, "image/jpeg");
// this will upload the file with public permission with specified mime type

上传文件函数有以下参数,请注意mime类型。UploadFile(源文件,ACL(公共|专用(,path_on_space_with_filename,mime_type(

附加提示:如果你想在太空中创建一个目录,只需使用

$path_to_file = "FOLDER_name/"; //dont miss the last slash
$space->UploadFile($path_to_file, "public","sub/folders/" . $path_to_file, 'text/directory'); // be noted for the mime type for the folder

相关内容

  • 没有找到相关文章

最新更新