You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求Oracle官方PHP SDK实现对象存储文件上传与URL分发方案

Re: Is there an official Oracle Cloud PHP SDK for uploading files/folders?

Great question—you’re not missing anything obvious, but the official Oracle Cloud Infrastructure (OCI) PHP SDK does exist, and it fully supports Object Storage operations like uploading files, simulating folders via prefixes, and generating accessible URLs for your app. Let’s break this down step by step:

1. Confirming the Official SDK

Oracle maintains an official PHP SDK for interacting with all OCI services, including Object Storage. It’s easy to overlook if you were searching for a standalone "Oracle Storage PHP SDK"—it’s bundled under the broader OCI PHP SDK suite, designed to work with all OCI resources.

2. Setup & Installation

First, make sure you have these prerequisites:

  • PHP 7.4 or higher (check Oracle’s compatibility notes for exact supported versions)
  • The oci8 PHP extension installed (required for low-level Oracle connectivity)
  • Composer for dependency management

Install the SDK via Composer:

composer require oracle/oci-sdk-php

3. Authentication Configuration

To interact with OCI, you’ll need to authenticate. The most common method for a PHP app is using API keys:

  1. Generate an RSA key pair on your server.
  2. Upload the public key to your OCI user profile.
  3. Create a config file (typically ~/.oci/config) with your credentials, or pass them programmatically in your code.

Example config snippet:

[DEFAULT]
user=ocid1.user.oc1..<unique_user_id>
fingerprint=<your_public_key_fingerprint>
tenancy=ocid1.tenancy.oc1..<unique_tenancy_id>
region=us-phoenix-1
key_file=~/.oci/oci_api_key.pem

4. Uploading a Single File

Here’s a basic example to upload a file to an Object Storage bucket:

use OCI\ObjectStorage\ObjectStorageClient;
use OCI\Common\Auth\ConfigFileAuthenticationDetailsProvider;

// Initialize the Object Storage client
$configProvider = new ConfigFileAuthenticationDetailsProvider();
$objectStorageClient = new ObjectStorageClient($configProvider);

// Define your bucket and file details
$namespaceName = "your_tenancy_namespace";
$bucketName = "your_bucket_name";
$objectName = "uploaded_file.txt";
$localFilePath = "/path/to/your/local/file.txt";

// Upload the file
$objectStorageClient->putObject(
    $namespaceName,
    $bucketName,
    $objectName,
    fopen($localFilePath, 'rb')
);

echo "File uploaded successfully!";

5. Uploading a Folder (Directory)

OCI Object Storage doesn’t use actual folders—instead, it uses prefixes to simulate directory structures. To upload a folder, iterate through all files in the directory and upload each with a prefix matching the folder path:

function uploadFolder($client, $namespace, $bucket, $localFolder, $prefix = "") {
    $dir = new DirectoryIterator($localFolder);
    foreach ($dir as $fileInfo) {
        if (!$fileInfo->isDot()) {
            if ($fileInfo->isDir()) {
                // Recursively upload subfolders
                uploadFolder($client, $namespace, $bucket, $fileInfo->getPathname(), $prefix . $fileInfo->getFilename() . "/");
            } else {
                $objectName = $prefix . $fileInfo->getFilename();
                $client->putObject(
                    $namespace,
                    $bucket,
                    $objectName,
                    fopen($fileInfo->getPathname(), 'rb')
                );
                echo "Uploaded: " . $objectName . "\n";
            }
        }
    }
}

// Usage
uploadFolder($objectStorageClient, $namespaceName, $bucketName, "/path/to/your/local/folder");

6. Generating Accessible URLs for Your App

There are two main ways to get a URL for your uploaded object:

Public Bucket URL

If your bucket is configured to allow public access, the URL follows this format:

https://<namespace>.objectstorage.<region>.oraclecloud.com/n/<namespace>/b/<bucket>/o/<object-name>

You can construct this URL directly once you know the object’s name and bucket details.

Pre-Authenticated Request (PAR) URL (for Private Buckets)

For private buckets, generate a time-limited PAR URL that grants access without requiring OCI credentials:

use OCI\ObjectStorage\Model\CreatePreauthenticatedRequestDetails;
use OCI\ObjectStorage\Model\PreauthenticatedRequestAccessType;

$parDetails = new CreatePreauthenticatedRequestDetails();
$parDetails->setObjectName($objectName);
$parDetails->setAccessType(PreauthenticatedRequestAccessType::OBJECT_READ);
$parDetails->setTimeExpires(new \DateTime('+7 days')); // Set your desired expiration time

$parResponse = $objectStorageClient->createPreauthenticatedRequest(
    $namespaceName,
    $bucketName,
    $parDetails
);

$accessUrl = $parResponse->getPreauthenticatedRequest()->getAccessUri();
echo "Accessible URL: " . "https://<namespace>.objectstorage.<region>.oraclecloud.com" . $accessUrl;

Troubleshooting Tips

  • If you struggled to find the SDK, double-check Oracle’s OCI Developer Tools section—it’s listed there alongside SDKs for other languages.
  • Ensure your OCI user has the necessary permissions (e.g., objectstorage:object:PutObject, objectstorage:bucket:CreatePreauthenticatedRequest) to perform these operations.

内容的提问来源于stack exchange,提问作者Akhilesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:03:34