Forum dédié au moteur de recherche et aux techniques d'optimisation par #taggle
Vous n'�tes pas identifi�.
Besoin d'aide pour comprendre ce que je dois faire avec ça ! Ça c'est le code PHP à utiliser pour pouvoir stocker ses fichiers sur Amazon et faire les différentes opérations.
Je dois créer un fichier à part avec ces instructions ou quoi d'autre (ref. http://docs.amazonwebservices.com/Amazo … 03-01/gsg/ )
$introMessage = "This file is a proof-of-concept of how to use Amazon S3 via PHP. It implements object PUT/GET/DELETE, bucket PUT(create)/GET(list)/DELETE, and service GET(list all my buckets). It sends your secret access key from your browser to your PHP server so <b>only use this over SSL or on a trusted network!</b>";
$S3_URL = "http://s3.amazonaws.com/";
// grab this with "pear install Crypt_HMAC"
require_once 'Crypt/HMAC.php';
// grab this with "pear install --onlyreqdeps HTTP_Request"
require_once 'HTTP/Request.php';
// Note that version HTTP_Request 1.3.0 has a BUG in it! Change line
// 765 from:
// (HTTP_REQUEST_METHOD_POST != $this->_method && empty($this->_postData) && empty($this->_postFiles))) {
// to:
// (HTTP_REQUEST_METHOD_POST == $this->_method && empty($this->_postData) && empty($this->_postFiles))) {
// Without this change PUTs with non-empty content-type will fail!
// generic form fields
$fields = array("AWSAccessKeyId" => "keyId",
"SecretAccessKey" => "secretKey",
"Bucket (no slashes)" => "bucket",
"Key" => "key",
"Content-Type" => "contentType");
// submit button value
$submit = $_REQUEST['submit'];
if ($submit =="") {
// The simplest case -- just draw the input form
?>
<html>
<head>
<script type="text/javascript" language="text/javascript">
<!--
function showPutFields() {
document.getElementById("putFile").style.visibility = 'visible';
document.getElementById("putCT").style.visibility = 'visible';
return false;
}
function hidePutFields() {
document.getElementById("putFile").style.visibility = 'hidden';
document.getElementById("putCT").style.visibility = 'hidden';
document.getElementById("fileName").value = '';
return false;
}
function putVisibility() {
if (document.getElementById("verb").options.selectedIndex == 1)
showPutFields();
else
hidePutFields();
return false;
}
-->
</script>
<title>S3/PHP proof-of-concept</title>
</head>
<body>
<h1>S3/PHP proof-of-concept</h1>
<? echo $introMessage ?>
<hr/>
<h1/>
<form action="s3.php" method="POST" enctype="multipart/form-data">
<table>
<?php
foreach ($fields as $k => $v) {
print "<tr><td>$k:</td><td><input name=\"$v\" value=\"\"/></td></tr>";
}
?>
<tr>
<td>Operation:</td>
<td>
<select id="verb" onchange="putVisibility();" name="verb">
<option value="GET">GET</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</td>
</tr>
<tr id="putCT" style="visibility:hidden;">
<td>Access Control Policy (PUT only):</td>
<td>
<select name="acl">
<option value="private">private</option>
<option value="public-read">public-read</option>
<option value="public-read-write">public-read-write</option>
</select>
</td>
</tr>
<tr id="putFile" style="visibility:hidden;">
<td colspan="2">File to PUT (PUT only): <input id="fileName" type="file" name="file"/></td>
</tr>
</table>
<input type="submit" name="submit" value="Go!"/>
</form>
<?php
ob_end_flush();
} elseif ($submit == "Go!") {
// User has entered parameters so let's do the S3 request!
// pull off request parameters.
$keyId = $_REQUEST['keyId'];
$secretKey = $_REQUEST['secretKey'];
$verb = $_REQUEST['verb'];
$acl = $_REQUEST['acl'];
$contentType = $_REQUEST['contentType'];
$resource = $_REQUEST['bucket'] . "/" . $_REQUEST['key'];
if ($resource == "/") {
$resource = "";
}
if ($verb == "PUT") {
if ($_REQUEST['key'] == "") {
$_FILES['file']['tmp_name'] = "";
}
if ($_FILES['file']['tmp_name'] == "" && $_REQUEST['key']!="") {
error("Must specify a file to use with object PUT");
}
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$filePath = $_FILES['file']['tmp_name'];
} elseif ($_FILES['file']['tmp_name'] != "") {
error("Serious error");
}
} else {
if ($_FILES['file']['tmp_name'] != "") {
error("Must not specify a file to use with anything but PUT");
}
}
$methods = array("GET"=>1, "DELETE"=>1, "PUT"=>1);
if ($methods[$verb] == 0) {
error("Unknown verb $verb");
}
$httpDate = gmdate(DATE_RFC822);
$stringToSign = "$verb\n\n$contentType\n$httpDate\nx-amz-acl:$acl\n/$resource";
$hasher =& new Crypt_HMAC($secretKey, "sha1");
$signature = hex2b64($hasher->hash($stringToSign));
// error("[$stringToSign,$signature]");
$req =& new HTTP_Request($S3_URL . $resource);
$req->setMethod($verb);
$req->addHeader("content-type", $contentType);
$req->addHeader("Date", $httpDate);
$req->addHeader("x-amz-acl", $acl);
$req->addHeader("Authorization", "AWS " . $keyId . ":" . $signature);
if ($filePath != "") {
$req->setBody(file_get_contents($filePath));
}
$req->sendRequest();
$ct = $req->getResponseHeader("content-type");
if ($ct == "application/xml") $ct = "text/xml";
header("content-type: $ct");
ob_end_flush();
if ($req->getResponseCode() >= 300) {
print $req->getResponseBody();
return;
}
if ($verb != "GET") {
print "$resource ${verb}ed successfully.";
return;
}
print $req->getResponseBody();
} else {
print "Unknown submit! [$submit]";
}
die;
function hex2b64($str) {
$raw = '';
for ($i=0; $i < strlen($str); $i+=2) {
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64_encode($raw);
}
function error($str) {
print "<div style='color: red;'><h2><pre>$str</pre></h2></div>";
die;
}
function dump($var) {
print "<pre>";
print_r($var);
print "</pre>";
}
?>Merci d'avance ![]()
Hors ligne
Bon, déjà, t'as besoin de 3 choses :
la librairie pear installée sur le serveur,
le fichier /Crypt/HMac.php sur ton serveur
le fichier /HTTP/Request.php sur ton serveur
déjà, est ce que t'as ca ?
Hors ligne
Anonymus, merci de ta réponse.
Je dispose de Red Hat 9 avec les services suivants :
Apache ASP support
SSI support
PHP support
CGI support
mod_perl support
mod_python support
A noter que pour effectuer les mêmes opérations qu'avec le code PHP ci-dessus je peux me servir des langages suivants :
# Java
# Perl
# PHP
# C#
# Python
# Ruby
# HTTP
En gros, ce que je veux faire est décrit ici : http://www.holovaty.com/blog/archive/2006/04/07/0927
Or en lisant cet article j'ai vu que lui utilise Python, alors je me suis dit que pour moi PHP était peut-être plus facile. Mais bon, ne connaissant ni l'un ni l'autre, peut-être que Java serait la solution, j'en ai vraiment aucune idée !
Le tout c'est de savoir par où commencer...
Hors ligne
T'as essayé, de mettre ca sur une page ?
C'est du code php. Tu le mets sur une page, et t'appelles la page, ca doit marcher tout seul.
Bon, faut comme même remplir le tableau ici :
$fields = array("AWSAccessKeyId" => "keyId",
"SecretAccessKey" => "secretKey",
"Bucket (no slashes)" => "bucket",
"Key" => "key",
"Content-Type" => "contentType");
avec tes données > remplaces KeyId par la clé que t'as donné Amazon pour te servir des kits programmes, etc..
Nico.
Hors ligne