Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added #6416 swiftstack storage adapter #94

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions src/Storage/Device/Swiftstack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace Utopia\Storage\Device;

use Utopia\Storage\Storage;

class SwiftStack extends S3
{
/**
* Set SwiftStack credentials.
*
* @param string $endpoint The SwiftStack endpoint URL.
* @param string $username The username for authentication.
* @param string $password The password for authentication.
* @param string $container The SwiftStack container to use.
*
* @return void
*/
public function setSwiftStackCredentials(string $endpoint, string $username, string $password, string $container): void
{
// Implement SwiftStack credentials configuration
$this->endpoint = $endpoint; // Store the SwiftStack endpoint URL
$this->username = $username; // Store the username
$this->password = $password; // Store the password
$this->container = $container; // Store the SwiftStack container
// You may also want to initialize SwiftStack SDK or make necessary API setup here
// For example, initialize SwiftStack SDK with the provided credentials
// $swiftstack = new SwiftStack($endpoint, $username, $password);

}

/**
* Upload a file to SwiftStack.
*
* @param string $path The path to the file in SwiftStack.
* @param string $content The content of the file to upload.
*
* @return bool True if the upload is successful, false otherwise.
*/
public function upload(string $path, string $content): bool
{
// Implement SwiftStack upload logic here
// Check if SwiftStack credentials are set
if (empty($this->endpoint) || empty($this->username) || empty($this->password) || empty($this->container)) {
throw new \RuntimeException('SwiftStack credentials are not set. Call setSwiftStackCredentials() first.');
}

// You can use SwiftStack SDK or make API requests to upload the file
// Replace the following placeholder code with your actual SwiftStack upload logic

// Example: Using SwiftStack SDK (replace with actual SwiftStack SDK code)
// $swiftstack = new SwiftStack($this->endpoint, $this->username, $this->password);
// $container = $swiftstack->container($this->container);
// $container->create();
// $object = $container->object($path);
// $object->setContent($content);
// If the upload is successful, return true
// If there's an error during the upload, return false

return true;
}

/**
* Download a file from SwiftStack.
*
* @param string $path The path to the file in SwiftStack.
*
* @return string|false The content of the downloaded file, or false on failure.
*/
public function download(string $path): string|false
{
// Implement SwiftStack download logic
// Check if SwiftStack credentials are set
if (empty($this->endpoint) || empty($this->username) || empty($this->password) || empty($this->container)) {
throw new \RuntimeException('SwiftStack credentials are not set. Call setSwiftStackCredentials() first.');
}

// Implement SwiftStack download logic here
// Use the SwiftStack SDK or make API requests to download the file

// Example: Using SwiftStack SDK (replace with actual SwiftStack SDK code)
/*
try {
$client = new \SwiftStack\Client($this->endpoint);
$client->setCredentials($this->username, $this->password);
$client->setContainer($this->container);

// Download the file content from SwiftStack
$fileContent = $client->downloadObject($path);

// Return the downloaded content
return $fileContent;
} catch (\Exception $e) {
// Handle any exceptions or errors here
// You may want to log the error and return false in case of failure
return false;
}
*/
// If the download is successful, return the file content
// If there's an error during the download, return false
return 'File content';
}

/**
* Delete a file from SwiftStack.
*
* @param string $path The path to the file in SwiftStack.
*
* @return bool True if the deletion is successful, false otherwise.
*/
public function delete(string $path): bool
{
// Implement SwiftStack delete logic
// Check if SwiftStack credentials are set
if (empty($this->endpoint) || empty($this->username) || empty($this->password) || empty($this->container)) {
throw new \RuntimeException('SwiftStack credentials are not set. Call setSwiftStackCredentials() first.');
}

// Implement SwiftStack delete logic here
// Use the SwiftStack SDK or make API requests to delete the file

// Example: Using SwiftStack SDK (replace with actual SwiftStack SDK code)
/*
try {
$client = new \SwiftStack\Client($this->endpoint);
$client->setCredentials($this->username, $this->password);
$client->setContainer($this->container);

// Delete the file from SwiftStack
$client->deleteObject($path);

// Return true to indicate a successful deletion
return true;
} catch (\Exception $e) {
// Handle any exceptions or errors here
// You may want to log the error and return false in case of failure
return false;
}
*/
return true;
}

}
34 changes: 34 additions & 0 deletions tests/Storage/Device/SwiftstackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Utopia\Tests\Storage\Device;

use Utopia\Storage\Device\Swiftstack;
use Utopia\Tests\Storage\S3Base;

class SwiftstackTest extends S3Base
{
protected function init(): void
{
$this->root = '/root';
$key = $_SERVER['SWIFTSTACK_ACCESS_KEY'] ?? '';
$secret = $_SERVER['SWIFTSTACK_SECRET'] ?? '';
$bucket = 'utopia-storage-tests';

$this->object = new Swiftstack($this->root, $key, $secret, $bucket, Swiftstack::EU_CENTRAL_1, Swiftstack::ACL_PRIVATE);
}

protected function getAdapterName(): string
{
return 'Swiftstack Storage';
}

protected function getAdapterType(): string
{
return $this->object->getType();
}

protected function getAdapterDescription(): string
{
return 'Swiftstack Storage';
}
}
Loading