title: How to get url of file uploaded on AWS S3 bucket?
body: <p>I am uploading image file on <strong>Amazon AWS S3 bucket</strong> using <strong>Java</strong>.</p>
  
  <p>I am using following maven dependency -     </p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
          &lt;artifactId&gt;aws-java-sdk&lt;/artifactId&gt;
          &lt;version&gt;1.9.28.1&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>I am able to upload the file, also able to download the contents. But I want to return the image url to the user. </p>
  
  <p>How can I get the image url immediately after uploading on s3 bucket?</p>
  
  <p>Here is my java code -</p>
  
  <pre><code>public void uploadProfile(InputStream fileInputStream, String fileName)  {      
  
          AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials("MY-ACCESS-KEY", "MY-SECRET-KEY"));
          String bucketName = "MyBucket";
          String key = "MyKey";
  
          try {
  
              System.out.println("Listing buckets");
              for (Bucket bucket : s3client.listBuckets()) {
                  System.out.println(" - " + bucket.getName());
              }
              System.out.println();
  
              System.out.println("Uploading a new object to S3 from a file\n");
  
              s3client.putObject(new PutObjectRequest(bucketName, key, fileInputStream, new ObjectMetadata()).withCannedAcl(CannedAccessControlList.PublicRead));
  
              System.out.println("Downloading an object");
              S3Object object = s3client.getObject(new GetObjectRequest(bucketName, key));
              System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
              displayTextInputStream(object.getObjectContent());
  
              System.out.println("Listing objects");
              ObjectListing objectListing = s3client.listObjects(new ListObjectsRequest()
                      .withBucketName(bucketName)
                      .withPrefix("My"));
              for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                  System.out.println(" - " + objectSummary.getKey() + "  " +
                                     "(size = " + objectSummary.getSize() + ")");
              }
              System.out.println();
  
           } catch (AmazonServiceException ase) {
              System.out.println("Error Message:    " + ase.getMessage());
              ase.printStackTrace();            
          } catch (AmazonClientException ace) {
              System.out.println("Error Message: " + ace.getMessage());
              ace.printStackTrace();
          }
      }
  
      private static void displayTextInputStream(InputStream input) throws IOException {
          BufferedReader reader = new BufferedReader(new InputStreamReader(input));
          while (true) {
              String line = reader.readLine();
              if (line == null) break;
  
              System.out.println("    " + line);
          }
          System.out.println();
      }
  </code></pre>
  
  <p>I have tried to hit the url : <code>https://MyBucket.s3.amazonaws.com/myfilename</code></p>
  
  <p>But its showing message in xml as -</p>
  
  <pre><code>&lt;Error&gt;
     &lt;Code&gt;AccessDenied&lt;/Code&gt;
     &lt;Message&gt;Access Denied&lt;/Message&gt;
     &lt;RequestId&gt;64358C14C976A2CB&lt;/RequestId&gt;
     &lt;HostId&gt;
        ZQ+DAaocOLxR/TmfEMGLcYm4Zk4Kl1vCKjciYiXKF1gYH5Lhj6oM4rd/o/4+SLVb
     &lt;/HostId&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>So how can I get the image url immediately after uploading on s3 bucket?</p>