0

I want to use AWS S3 to store image files for my website. I create a bucket name images.mydomain.com which was referred by dns cname images.mydomain.com from AWS Route 53.

I want to check whether a folder or file exists; if not I will create one.
The following php codes work fine for regular bucket name using stream wrapper but fails for this type of bucket name such as xxxx.mydomain.com. This kind of bucket name fails in doesObjectExist() method too.

// $new_dir = "s3://aaaa/akak3/kk1/yy3/ww4" ;   //  this line works ! 

$new_dir = "s3://images.mydomain.com/us000000/10000" ;   // this line fails ! 

if( !file_exists( $new_dir) ){
   if( !mkdir( $new_dir , 0777 , true ) ) {
         echo "create new dir  $new_dir failed ! <br>" ;
   } else { 
        echo "SUCCEED in creating  new dir $new_dir <br" ; 
   }
} else { 
  echo "dir $new_dir already exists. Skip creating dir ! <br>" ;  
} 

I got the following message

Warning: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint: "images.mydomain.com.s3.amazonaws.com". in C:\AppServ\www\ecity\vendor\aws\aws-sdk-php\src\Aws\S3\StreamWrapper.php on line 737

What is the problem here?
Any advise on what to do for this case?
Thanks!

user2818066
  • 618
  • 2
  • 8
  • 19
  • If it's not in us-east-1, have you tried using the correct region for your bucket? http://docs.aws.amazon.com/aws-sdk-php/guide/latest/configuration.html#specify-region – Michael - sqlbot Jul 14 '14 at 23:23
  • It is us-west-2. I did not specify region when calling S3. But as it was pointed out in the source codes above. If I use the bucket name aaaa line, the codes works. If I used the bucket name images.mydomain.com, the codes produce error message. But both bucket names aaaa and images.mydomain.com exist in the same s3 folder which means they are in the same region. – user2818066 Jul 15 '14 at 00:36
  • The S3 console, if that's what you're referring to, shows all buckets in all regions. The error you are getting is a permanent redirect, suggesting you need to explicitly select the region. – Michael - sqlbot Jul 15 '14 at 02:26
  • Both buckets have end points with **us-west-2**. AWS console says Global -> "S3 does not require region selection". It looks like the only difference is xx.mydomain.com is referred by a cname in route35 but aaaa bucket is not. – user2818066 Jul 15 '14 at 04:15
  • Right... In the console, it doesn't require a region selection. In the client, you should use the correct region. – Michael - sqlbot Jul 15 '14 at 10:26
  • Thanks! Yes, you are right. I need to specify region in s3 instantiation when access xxx.mydomain.com. Another solution is that I can redirect bucket name xxx.mydomain.com to bucket name s3-us-west-2.amazonaws.com/aaaa and store data in bucket aaaa then I don't need to specify the region. However in this case will I encounter problem in the future if I continue neglecting the specification of region in s3 instantiation when the app scale up to multiple regions? – user2818066 Jul 16 '14 at 01:49

0 Answers0