如何使用PHP SDK上传文件至Oracle Cloud Storage?未找到官方SDK求助
Hey there! I totally get where you're coming from—Oracle does not provide an official PHP SDK for their Object Storage service, so you haven't missed anything in your research. Let's break down practical, working approaches to upload content and access file URLs using PHP:
1. Directly Call the Oracle Object Storage REST API
This is the most reliable method since it uses Oracle's native API endpoints. Here's how to implement it:
Prerequisites First
Before writing code, you'll need these credentials from your Oracle Cloud account:
- Tenancy OCID
- User OCID
- Region identifier (e.g.,
us-phoenix-1) - API Key Fingerprint
- Private key file (PEM format) generated for your user
Example: Upload a Single File
You can use PHP's curl extension or a library like Guzzle for cleaner code. Here's a basic curl-based example (note: the signature generation is simplified—you'll need to implement full signing logic per Oracle's docs):
<?php $tenancyId = 'your-tenancy-ocid'; $userId = 'your-user-ocid'; $fingerprint = 'your-api-key-fingerprint'; $privateKeyPath = '/path/to/your/private-key.pem'; $region = 'us-phoenix-1'; $bucketName = 'your-bucket-name'; $objectName = 'uploaded-file.txt'; $filePath = '/path/to/local/file.txt'; // Load private key $privateKey = file_get_contents($privateKeyPath); // Generate authorization signature (follow Oracle's full signature guidelines for production) $authHeader = generateOracleAuthHeader($tenancyId, $userId, $fingerprint, $privateKey, $region); // Initialize curl upload $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://objectstorage.$region.oraclecloud.com/n/$tenancyId/b/$bucketName/o/$objectName"); curl_setopt($ch, CURLOPT_PUT, 1); curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r')); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: $authHeader", "Content-Type: application/octet-stream" ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // Check upload status if ($httpCode === 200 || $httpCode === 201) { echo "File uploaded successfully!\n"; // Get public URL (if bucket is configured for public access) $publicUrl = "https://objectstorage.$region.oraclecloud.com/n/$tenancyId/b/$bucketName/o/$objectName"; echo "File URL: $publicUrl"; } else { echo "Upload failed. HTTP Code: $httpCode. Response: $response"; } // Simplified helper for auth header (replace with full signature logic per Oracle docs) function generateOracleAuthHeader($tenancyId, $userId, $fingerprint, $privateKey, $region) { $date = gmdate('D, d M Y H:i:s T'); $signString = "date: $date"; openssl_sign($signString, $signature, $privateKey, OPENSSL_ALGO_SHA256); $encodedSignature = base64_encode($signature); return "Signature version=\"1\",keyId=\"$tenancyId/$userId/$fingerprint\",algorithm=\"rsa-sha256\",headers=\"date\",signature=\"$encodedSignature\""; } ?>
Uploading Entire Folders
To upload a folder, add a loop to traverse all files in the directory and upload each one individually:
<?php $localFolderPath = '/path/to/local/folder'; $bucketPrefix = 'cloud-folder-prefix/'; // Organize files under this prefix in the bucket // Traverse directory recursively $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($localFolderPath)); foreach ($iterator as $file) { if ($file->isFile()) { // Get relative path to preserve folder structure $relativePath = str_replace(rtrim($localFolderPath, '/') . '/', '', $file->getPathname()); $objectName = $bucketPrefix . $relativePath; // Reuse the upload function from the single file example uploadFileToOracle($file->getPathname(), $objectName); } } ?>
Accessing File URLs
- Public Buckets: If your bucket allows public access, use this format:
https://objectstorage.[region].oraclecloud.com/n/[tenancy-id]/b/[bucket-name]/o/[object-name] - Private Buckets: Generate a pre-signed URL via the REST API (sign a GET request with an expiration time) to grant temporary, secure access.
2. Use Community-Maintained PHP Libraries
While not official, there are community packages that wrap Oracle's API to simplify development. For example:
- Install via Composer:
composer require oracle/cloud-infrastructure-php - Note: Always verify the package's maintenance status and security before using it in production.
Key Notes
- Ensure your Oracle Cloud user has the correct IAM policies to upload objects (e.g.,
allow user [user-ocid] to manage objects in tenancy where target.bucket.name='your-bucket-name'). - For production, add error handling, retry logic for failed uploads, and secure storage of your private key.
内容的提问来源于stack exchange,提问作者Akhilesh

