如何创建临时 URL 以使用 PHP 将文件上传到 RackSpace 云文件?
How to create Temporary URL to upload file to RackSpace Cloud Files using PHP?
我有这段代码可以从 rackspace 云文件中获取文件:
$username = getenv('RS_USERNAME');
$apikey = getenv('RS_APIKEY');
$containerName = 'imagenesfc';
$region = 'DFW';
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => $username,
'apiKey' => $apikey,
));
$filename = "myfile.ext";
$service = $client->objectStoreService(null, $region);
$container = $service->getContainer($containerName);
$object = $container->getObject($filename);
$account = $service->getAccount();
$account->setTempUrlSecret();
$tempUrl = $object->getTemporaryUrl(15, 'GET');
在open php cloud documentation says you can change 'GET' to 'PUT' to what I imagine is being able to put a file, instead of getting it, the problem is that the file doesn't exist yet, and apparently the only way to create a file is uploading it first? PHP SDK Container
在 Amazon s3 中,我可以执行以下操作来获得我想要的内容:
$keyname = "myfile.ext";
$arr = array(
'Bucket' => 'bucket',
'Key' => $keyname
);
$cmd = $s3Client->getCommand('PutObject', $arr);
$request = $s3Client->createPresignedRequest($cmd, '+10 minutes');
$presignedUrl = (string) $request->getUri();
然后我可以按照我喜欢的方式写入 presignedUrl,例如 Java:
url = new URL(jObj.get("presignedUrl").toString().replace("\", ""));
connection=(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
out = new DataOutputStream(connection.getOutputStream());
...(write,flush,close)
所以,基本上我想做的是从 RackSpace 上传 URL 并像我使用 Amazon s3 一样写入它。
可能吗?如果是,你该怎么做?
我需要这样做,因为我的 API 将只提供下载和上传链接,所以没有流量直接通过它。我无法将文件保存到我的 API 服务器,然后将其上传到云端。
是的,您可以模拟文件上传而无需实际将内容上传到 API - 您需要做的就是确定文件名。您需要的代码是:
$object = $container->getObject();
$object->setName('blah.txt');
$account = $service->getAccount();
$account->setTempUrlSecret();
echo $object->getTemporaryUrl(100, 'PUT');
getObject
方法 returns 一个空对象,您所做的只是在该对象上设置远程名称。创建临时 URL 时,它会使用您刚刚设置的名称并提供一个临时 URL 供您使用 - 无论该对象是否存在于远程。然后可以使用临时 URL 创建对象。
我有这段代码可以从 rackspace 云文件中获取文件:
$username = getenv('RS_USERNAME');
$apikey = getenv('RS_APIKEY');
$containerName = 'imagenesfc';
$region = 'DFW';
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => $username,
'apiKey' => $apikey,
));
$filename = "myfile.ext";
$service = $client->objectStoreService(null, $region);
$container = $service->getContainer($containerName);
$object = $container->getObject($filename);
$account = $service->getAccount();
$account->setTempUrlSecret();
$tempUrl = $object->getTemporaryUrl(15, 'GET');
在open php cloud documentation says you can change 'GET' to 'PUT' to what I imagine is being able to put a file, instead of getting it, the problem is that the file doesn't exist yet, and apparently the only way to create a file is uploading it first? PHP SDK Container
在 Amazon s3 中,我可以执行以下操作来获得我想要的内容:
$keyname = "myfile.ext";
$arr = array(
'Bucket' => 'bucket',
'Key' => $keyname
);
$cmd = $s3Client->getCommand('PutObject', $arr);
$request = $s3Client->createPresignedRequest($cmd, '+10 minutes');
$presignedUrl = (string) $request->getUri();
然后我可以按照我喜欢的方式写入 presignedUrl,例如 Java:
url = new URL(jObj.get("presignedUrl").toString().replace("\", ""));
connection=(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
out = new DataOutputStream(connection.getOutputStream());
...(write,flush,close)
所以,基本上我想做的是从 RackSpace 上传 URL 并像我使用 Amazon s3 一样写入它。
可能吗?如果是,你该怎么做?
我需要这样做,因为我的 API 将只提供下载和上传链接,所以没有流量直接通过它。我无法将文件保存到我的 API 服务器,然后将其上传到云端。
是的,您可以模拟文件上传而无需实际将内容上传到 API - 您需要做的就是确定文件名。您需要的代码是:
$object = $container->getObject();
$object->setName('blah.txt');
$account = $service->getAccount();
$account->setTempUrlSecret();
echo $object->getTemporaryUrl(100, 'PUT');
getObject
方法 returns 一个空对象,您所做的只是在该对象上设置远程名称。创建临时 URL 时,它会使用您刚刚设置的名称并提供一个临时 URL 供您使用 - 无论该对象是否存在于远程。然后可以使用临时 URL 创建对象。