Add pure_php_neo.php
This commit is contained in:
parent
d4d867c277
commit
768e93bc98
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
// pure_php_neo.php
|
||||
require_once 'vendor/autoload.php'; // Composer AWS SDK
|
||||
|
||||
use Aws\S3\S3Client;
|
||||
use Aws\Exception\AwsException;
|
||||
|
||||
class NeoObjectStorage {
|
||||
private $s3Client;
|
||||
private $bucket;
|
||||
|
||||
public function __construct($access_key, $secret_key, $endpoint, $bucket) {
|
||||
$this->bucket = $bucket;
|
||||
|
||||
$this->s3Client = new S3Client([
|
||||
'version' => 'latest',
|
||||
'region' => 'wjv-1',
|
||||
'endpoint' => $endpoint,
|
||||
'credentials' => [
|
||||
'key' => $access_key,
|
||||
'secret' => $secret_key,
|
||||
],
|
||||
'use_path_style_endpoint' => true,
|
||||
'http' => [
|
||||
'verify' => false // Disable SSL verification
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadFile($file, $key) {
|
||||
try {
|
||||
$result = $this->s3Client->putObject([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $key,
|
||||
'SourceFile' => $file,
|
||||
'ACL' => 'public-read'
|
||||
]);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'url' => $result['ObjectURL']
|
||||
];
|
||||
} catch (AwsException $e) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function listFiles($prefix = '') {
|
||||
try {
|
||||
$result = $this->s3Client->listObjectsV2([
|
||||
'Bucket' => $this->bucket,
|
||||
'Prefix' => $prefix
|
||||
]);
|
||||
|
||||
$files = [];
|
||||
foreach ($result['Contents'] ?? [] as $object) {
|
||||
$files[] = [
|
||||
'key' => $object['Key'],
|
||||
'size' => $object['Size'],
|
||||
'modified' => $object['LastModified']->format('Y-m-d H:i:s')
|
||||
];
|
||||
}
|
||||
|
||||
return ['success' => true, 'files' => $files];
|
||||
} catch (AwsException $e) {
|
||||
return ['success' => false, 'error' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Usage
|
||||
$neo = new NeoObjectStorage(
|
||||
'your_access_key',
|
||||
'your_secret_key',
|
||||
'http://nos.wjv-1.neo.id',
|
||||
'your-bucket'
|
||||
);
|
||||
|
||||
// Upload
|
||||
if ($_FILES['file']) {
|
||||
$key = 'uploads/' . time() . '_' . $_FILES['file']['name'];
|
||||
$result = $neo->uploadFile($_FILES['file']['tmp_name'], $key);
|
||||
echo json_encode($result);
|
||||
}
|
||||
?>
|
||||
Loading…
Reference in New Issue