gpt4 book ai didi

rackspace-cloud - 从RackSpace文件下载文件

转载 作者:行者123 更新时间:2023-12-03 23:36:34 24 4
gpt4 key购买 nike

我已经使用composer安装了PHP SDK。看来samples文件夹中有旧代码,导致它不适用于我。我想从RackSpace文件夹下载一些文件。以下是我的代码,没有任何帮助。

<?php
require 'vendor/autoload.php';

$authURL = 'https://identity.api.rackspacecloud.com/v2.0/';
$credentials = array(
'username' => 'XXXX',
'apiKey' => 'XXXX'
);
$connection = new \OpenCloud\Rackspace($authURL, $credentials);
// var_dump($connection);exit;
$objstore = $connection->objectStoreService('cloudFiles', 'DFW');

// get our containers
print("Containers:\n");
$conlist = $objstore->listContainers();
//var_dump($conlist);
while($container = $conlist->Next()) {
printf("* %s\n", $container->name);
}

最佳答案

首先,更新到最新版本的php-opencloud,当前为1.7。

接下来,位于对象存储库的示例代码位于here,但不包括您要执行的操作。

以下给出路径的代码将遍历容器,并将对象从容器保存到目标路径($ savePath)。如果该路径中已经存在该对象,则将跳过该对象。此版本包含指示每个对象成功或失败的输出。试试看,如果您有任何问题,请告诉我。

注意:请记住,Rackspace的云文件(对象存储)是在每个数据中心的基础上处理的,因此仅当您在ORD中连接到objectStoreService时,才可以访问ORD容器中存储的文件。

<?php
require 'vendor/autoload.php';

$authURL = 'https://identity.api.rackspacecloud.com/v2.0/';
$credentials = array(
'username' => 'YOUR_USERNAME',
'apiKey' => 'YOUR_API_KEY',
);

$savePath = '/path/to/files/';
$connection = new \OpenCloud\Rackspace($authURL, $credentials);

$objstore = $connection->objectStoreService('cloudFiles', 'ORD');

// get our containers
print("Containers:\n");
$conlist = $objstore->listContainers();
//var_dump($conlist);
while($container = $conlist->Next()) {
printf("*** %s\n", $container->name);
if($container->name == 'test2')
{
$files = $container->ObjectList();
while($o = $files->Next())
{
$file_name = $o->getName();
// Get our object
$file = $container->getObject($file_name);
printf("** %s\n", $file->getName());
// Let's save this file
echo "* Saving object\n";
if(file_exists($savePath.$file_name))
{
echo "* File already exists! SKIPPING\n\n";
}
else
{
if (!$fp = @fopen($savePath.$file_name, "wb")) {
throw new OpenCloud\Common\Exceptions\IOError(sprintf(
'Could not open file [%s] for writing',
$savePath.$file_name
));
}
//$retval = fwrite($fp, $o->getContent());
if (fwrite($fp, $file->getContent()) === FALSE) {
echo "* ERROR - Cannot write to file ($savePath.$file_name)\n\n";
}
else
{
echo "* File successfully written\n\n";
}
}
}
}
}


输出:

Containers:
*** gallery
*** test2
** 61OUUC44G-L._SL1471_.jpg
* Saving object
* File written

** Computer-Code.jpg
* Saving object
* File written

** accessibility2.jpg
* Saving object
* File written


我的服务器上的目录列表:

root@app01:/path/to/files# ll
total 960
drwxrwxrwx 2 root root 4096 Nov 8 18:53 ./
drwxr-xr-x 15 www-data www-data 4096 Nov 8 18:20 ../
-rw-r--r-- 1 www-data www-data 68650 Nov 8 18:45 61OUUC44G-L._SL1471_.jpg
-rw-r--r-- 1 www-data www-data 374177 Nov 8 18:45 accessibility2.jpg
-rw-r--r-- 1 www-data www-data 515919 Nov 8 18:45 Computer-Code.jpg

关于rackspace-cloud - 从RackSpace文件下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com