Error When Creating New Buckets in PHP

dreamobjects

#1

Hello, I am working on a custom website where our clients and employees can upload, download and share files on DreamObjects. So far so good using the SDK provided until today.

I tried to to add a script to allow users to add new buckets and I am getting an error.

Here is my code:
$client->createBucket(array(‘Bucket’ => $_POST[‘newbucketname’]));

Here is my error:
Fatal error: Call to a member function createBucket() on a non-object in /home/xxx/xxx.com/registered.php on line 22

Do you know what I am doing wrong?

Up until this point I have been able to view buckets and objects within a bucket once it is selected. All this activity happens on the same php page. Thanks!


#2

That error message indicates that $client is not set, or is set to an inappropriate value. You’d need to post more of your code for us to debug it.

Also, keep in mind that bucket names are shared across all DreamObjects users — not all names input by your users will be available! You may want to use a unique prefix for your bucket names to minimize conflicts. (For instance, instead of “newbucketname”, you might give a new bucket a name like “soflyhoney-newbucketname”.)


#3

OK So here is where I setup the client. I also included code that I have in there that works properly in SELECT YOUR BUCKETS area.

Mindfield's Client Filesharing System

Login Required

<div class="container">

<?php
define('AWS_KEY', $_SESSION['accesskey']);
define('AWS_SECRET_KEY', $_SESSION['secretkey']);
define('HOST', 'https://objects.dreamhost.com');

// require the AWS SDK for PHP library
require 'aws-autoloader.php';

use Aws\S3\S3Client;
// Establish connection with DreamObjects with an S3 client.
$client = S3Client::factory(array(
    'base_url' => HOST,
    'key'      => AWS_KEY,
    'secret'   => AWS_SECRET_KEY
));

if($_SESSION['id']){
echo '<h3>Hello, '.$_SESSION['usr'].'! You are registered and logged in!</h3><br>';

if($_SESSION['selectedbucket']){
	echo '<h3>You are currently working in bucket: '.$_SESSION['selectedbucket'].'</h3><br><br>';
	}


//***********************SELECT YOUR BUCKET!!!***************************************************************
echo '<div class="bucketselector">';
echo '<h3>Bucket Selector</h3>';
echo '<div class="h1">Select The Bucket You Would Like to Upload To or Download From:</div>';
$blist = $client->listBuckets();
echo '<form action="" method="post">';
//echo "   Buckets belonging to " . $blist['Owner']['ID'] . ":\n";

foreach ($blist['Buckets'] as $b) {

	$bucketname = $b['Name'];
	echo '<input class="button" type=button value='.$bucketname.' onClick=selectThisBucket("' . $bucketname. '")><br>';
	//echo "{$b['Name']}<br>";
}
echo '<br><input class="bucketinput" type="text" name="bucketname" id="bucketname" value="" size="30" readonly/><br>';
echo '<input type="submit" name="submit" value="SELECT BUCKET" class="submitbutton" label="SELECT BUCKET"/><br>';
echo '</form>';
echo '</div><br><br><br>';
}
else echo '<h3>Please, <a href="index.php">login</a> to use system!</h3>';
	
//***********************ADD NEW BUCKET!!!***************************************************************
if($_SESSION['id']){
	echo '<div class="bucketadder">';
	echo '<h3>Add a New Bucket </h3>';
	echo '<form action="" method="post">';
	echo '<div class="h1">Type in the name of your new bucket. <br> Dashes are ok, but keep it lowercase and simple as possible.</div>';
	echo '<input class="newbucketinput" type="text" name="newbucketname" id="newbucketname" value="" size="40" />';
	echo '<input type="submit" name="submit" value="ADD BUCKET" class="submitbutton" />';
	echo '</form>';
	echo '</div>';

}

FROM HERE you can see I post the data from the field newbucketname and that calls this code on the post. The SELECT BUCKET post works fine, the ADD BUCKET post gives me that error. I am sure I am doing something really dumb… I’m not new to coding but I’m not very rehearsed in PHP… thanks so much!!!

if($_POST[‘submit’]==‘SELECT BUCKET’)
{
//alert(“submit”.$_POST[‘bucketname’]);
$_SESSION[‘selectedbucket’] = $_POST[‘bucketname’];

header("Location: registered.php");
exit;

}
if($_POST[‘submit’]==‘ADD BUCKET’)
{
$_SESSION[‘selectedbucket’] = $_POST[‘newbucketname’];
$client->createBucket(array(‘Bucket’ => $_POST[‘newbucketname’]));
header(“Location: registered.php”);
exit;
}


#4

Is the code that’s displaying that error in the same file as the code that sets $client? If not, you may need to duplicate that code there (or move it into a shared file).


#5

ok i figured it out! the code was in the same file but it was up at the top of the document before the header and before client value was getting set. so i moved the code below where client gets set and removed the header references in the code since the page submits to itself, it didn’t need the header info in there anyway! its working now! THANK YOU!