简体   繁体   中英

Upload zip file or image to Amazon s3 using java and html input form

Hope you can help me out. I couldn't find any answers to my question

I've got this part of JSP code:

  <html:form action="/restricted/gallery/GalleryUpload" method="post" enctype="multipart/form-data">  
  <table width="98%" border="0" cellspacing="0" cellpadding="2">
    <tr>
      <td width="21%" align="right" class="input_head">Upload Image: &nbsp;</td>
      <td class="size12_text_red"><html:file property="file" styleId="File"/>&nbsp;<br/>(Contents of zip files will be extracted and uploaded individually)</td>
    </tr>
    <tr align="center">
      <td colspan="4"><input name="upload2" type="submit" id="upload" class="butt_style" value="Submit"/></td>
    </tr>
  </table>

Then I i'm passing the data through to the Java class:

            else if (req.getParameter("upload2") != null) {

            String existingBucketName = "BUCKETNAME";           
            String filePath = cform.getFile().toString(); 
            String keyName = filePath.substring(filePath.lastIndexOf("/")+1); 

            String amazonFileUploadLocationOriginal=existingBucketName+"/gallery/"+user.getOwnerId()+"/album_name"; 
            AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(SouthdownsServicesAction.class.getResourceAsStream("AwsCredentials.properties"))); 
            FileInputStream stream = new FileInputStream(filePath); 
            ObjectMetadata objectMetadata = new ObjectMetadata(); 
            PutObjectRequest putObjectRequest = new PutObjectRequest(amazonFileUploadLocationOriginal, keyName, stream, objectMetadata); 
            PutObjectResult result = s3Client.putObject(putObjectRequest); 

So what i'm trying to do is, in the filePath section i'm trying to pass the full path from the input form file you've selected to there but it simply doesn't work. If i change that filePath to a simple path located on my local machine it works fine. Like the following:

String filePath = "C://Pics//logo.jpg"; 

Then if i upload a ZIP file instead of an image how will it extract the ZIP content and upload all files individually?

Thanks in advance

Please use the put() method below. S3 will not unzip your files - you will have to upload then one by one if you want them to appear separately in S3.

    //---------------------------------------------------------------------     
    // Amazon S3
    //---------------------------------------------------------------------     
        class S3 extends AmazonS3Client
         {final String bucket;
          S3(String u, String p, String Bucket)
           {super(new BasicAWSCredentials(u, p));
            bucket = Bucket;
           }
          boolean put(String k, String v)      
           {try 
             {final ByteArrayInputStream b = new ByteArrayInputStream(v.toString().getBytes());
              putObject(bucket, k, b, new ObjectMetadata());
              setObjectAcl(bucket, k, CannedAccessControlList.PublicRead);  // Has to be here to allow change to reduced redundancy
              changeObjectStorageClass(bucket, k, StorageClass.ReducedRedundancy);
              setObjectAcl(bucket, k, CannedAccessControlList.PublicRead);  // Has to be repeated because it is now a new object again
              return true; 
             }
            catch(Exception e) {log("Cannot put "+bucket+"/"+k+" to S3 because "+e);}
            return false; 
           }
          String get(String k) 
           {try 
             {final S3Object f = getObject(bucket, k);
              final BufferedInputStream i = new BufferedInputStream(f.getObjectContent());  
              final StringBuilder s = new StringBuilder(); 
              final byte[]b = new byte[1024];
              for(int n = i.read(b); n != -1; n = i.read(b)) {s.append(new String(b, 0, n));}
              return s.toString(); 
             }
            catch(Exception e) {log("Cannot get "+bucket+"/"+k+" from S3 because "+e);}
            return null; 
           }
          String[]list(String d) 
           {try 
             {final ObjectListing l = listObjects(bucket, d);
              final List<S3ObjectSummary> L = l.getObjectSummaries(); 
              final int n = L.size();
              final String[]s = new String[n];  
              for(int i = 0; i < n; ++i)
               {final S3ObjectSummary k = L.get(i);
                s[i] = k.getKey();
               } 
              return s; 
             }
            catch(Exception e) {log("Cannot list "+bucket+"/"+d+" on S3 because "+e);}
            return new String[]{}; 
           }
         }
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM