-
Notifications
You must be signed in to change notification settings - Fork 1
/
connectS3.php
148 lines (129 loc) · 5.56 KB
/
connectS3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* ---------------------- Amazon S3 Model Class ------------------------
* Author: Saurav Majumder
* Date: Nov 11, 2013
* Description: This class can be used as a model class for interaction with
* Amazon S3 for creating bucket, retrieving bucket information, creating
* file, reading file and writing file.
*
* @param AWS Access Key and AWS Secret Key are needed to be included in the
* variables mentioned below.
* --------------------------------------------------------------------
*/
require 'vendor/autoload.php'
use Aws\S3\S3Client;
class connectS3 {
private $current_bucket; // Variable for current bucket in use.
private $aws_access_key_id = ""; // Put the AWS Access Key of your account.
private $aws_secret_key = ""; // Put the AWS Secret Key of your account.
function __construct() {
// Establish connection with the Amazon S3 with provided credentials.
$client = S3Client::factory(array(
'key' => $aws_access_key_id,
'secret' => $aws_secret_key
));
}
// Function to create new bucket in the Amazon S3
// @param : new bucket name
// @result : Return true if bucket is created or return false.
function createBucket($bucket_name) {
if($bucket_name == "") return false;
try {
$client->createBucket(array('Bucket' => $bucket_name)); // Create new bucket with provided name.
$client->waitUntilBucketExists(array('Bucket' => $bucket_name)); // Check if the new bucket has been created.
$current_bucket = $bucket_name; // Assign this new bucket as the current bucket.
return true; // Return true when bucket is created.
} catch (Exception $e) {
return false; // Return false when bucket is not created.
}
}
// Function to create bucket in a specific region.
// @param : (1) new bucket name, (2) Region (Amazon Specified)
// @result : Return true if the bucket is created or return false.
function createBucket($bucket_name, $location) {
if($bucket_name == "" || $location == "") return false; // Check for empty input arguements.
try {
$client->createBucket(array(
'Bucket' => $bucket, // Create new bucket.
'LocationConstraint' => \Aws\Common\Enum\Region::$location
));
$client->waitUntilBucketExists(array('Bucket' => $bucket_name)); // Check if the new bucket has been created.
$current_bucket = $bucket_name; // Assign this new bucket as the current bucket.
return true; // Return true when bucket is created.
} catch (Exception $e) {
return false; // Return false if the bucket is not created.
}
}
// Function to retrieve all available buckets.
// @param : None
// @result : Array of bucket names.
function getAllBucketName() {
$array[] = $bucket_names; // Initialize array for bucket names.
$result = $client->listBuckets(); // Get list of buckets from Amazon S3.
foreach ($result['Buckets'] as $bucket) {
array_push($bucket_names, $bucket['Name']); // Put bucket names in the array.
}
return $bucket_names; // Return the array of backet names.
}
// Function to assign a certain bucket as current bucket in use.
// @param : Name of bucket to be used.
// @result : Return true if the bucket is found and assigned or return false.
function useBucket($bucket_name) {
if($bucket_name == "") return false; // Input argument validation.
try {
$client->waitUntilBucketExists(array('Bucket' => $bucket_name)); // Find the specified bucket.
$current_bucket = $bucket_name; // Assign the bucket as the current bucket.
return true; // Return true when completed.
} catch (Exception $e) {
return false; // Return false if the opretion failed.
}
}
// Function to upload a file to Amazon S3.
// @param : (1) Absolute path of the file to be uploaded (2) Name of that file in S3.
// @result : Return the URL of the uploaded file in Amazon S3.
function uploadFile($filePath, $fileName) {
if($filePath == "" || $fileName == "") return ; // Input argument validation.
if($current_bucket == "") return false; // Check if current bucket is assigned.
try {
$result = $client->putObject(array( // Upload file with provided URL.
'Bucket' => $current_bucket,
'Key' => $fileName,
'SourceFile' => $filePath,
'Metadata' => array(
'Author' => 's3-model', // Uploaded file author set to 's3-model'
)
));
$client->waitUntilObjectExists(array( // Check if the file has been uploaded or not.
'Bucket' => $current_bucket,
'Key' => $fileName
));
} catch (Exception $e) {
return false;
}
return $result['ObjectURL']; // Return the URL of the uploaded file.
}
// Function to read a file from Amazon S3.
// @param : file name.
// @result : String containing the content of the file.
function readFile($fileName) {
if($fileName == "") return null; // Input argument validation.
$result = $client->getObject(array( // Retrieve the object S3.
'Bucket' => $current_bucket,
'Key' => $fileName
));
return $result['Body']; // Return the content of the file
}
// Function to write to a specific file.
// @param : (1) File name, (2) Content
// @result : True once the write is completed or return false.
function writeFile($fileName, $data) {
if($fileName == "") return false;
$result = $client->putObject(array(
'Bucket' => $current_bucket,
'Key' => 'data.txt',
'Body' => $data
));
}
}
?>