2016-04-18 8 views
0

Ich bin auf der Suche nach einem Dienst zum Hochladen von Videos und Bildern von meinen mobilen Anwendungen (Frontend). Ich habe von Amazon S3 und CloudFront gehört. Ich suche nach einem Dienst, der sie speichert, und kann auch prüfen, ob sie bestimmte Kriterien erfüllen (z. B. maximale Dateigröße von 3 MB pro Bild) und einen Fehler an den Client zurücksenden, wenn die Datei nicht übereinstimmt das Kriterium. Bietet Amazon S3 oder CloudFront dies? Wenn nicht, gibt es einen anderen empfohlenen Service dafür?Dienst zum Hochladen von Bildern und Videos (S3, CloudFront usw.)

Antwort

2

Sie könnten das AWS SDK verwenden. Hier folgt ein Beispiel für die Java-Version (Amazon bietet SDKs für verschiedene Sprachen):

/** 
* It stores the given file name in S3 and returns the key under which the file has been stored 
* @param resource 
* @param bucketName 
* @return 
*/ 
public String storeProfileImage(File resource, String bucketName, String username) { 

    String resourceUrl = null; 

    if (!resource.exists()) { 
     throw new IllegalArgumentException("The file " + resource.getAbsolutePath() + " doesn't exist"); 

    } 

    long lengthInBytes = resource.length(); 

    //For demo purposes. You should use a configurable property for the max size 
    if (lengthInBytes > (3 * 1024)) { 
     //Your error handling here 
    } 

    AccessControlList acl = new AccessControlList(); 
    acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); 

    String key = username + "/profilePicture." + FilenameUtils.getExtension(resource.getName()); 

    try { 
     s3Client.putObject(new PutObjectRequest(bucketName, key, resource).withAccessControlList(acl)); 
     resourceUrl = s3Client.getResourceUrl(bucketName, key); 
    } catch (AmazonClientException ace) { 
     LOG.error("A client exception occurred while trying to store the profile" + 
       " image {} on S3. The profile image won't be stored", resource.getAbsolutePath(), ace); 
    } 

    return resourceUrl; 

} 

Sie können auch andere Operationen durchführen, zum Beispiel Überprüfen Sie, ob der Eimer existiert, bevor Sie das Bild speichern

/** 
* Returns the root URL where the bucket name is located. 
* <p>Please note that the URL does not contain the bucket name</p> 
* @param bucketName The bucket name 
* @return the root URL where the bucket name is located. 
*/ 
public String ensureBucketExists(String bucketName) { 

    String bucketUrl = null; 

    try { 
     if (!s3Client.doesBucketExist(bucketName)) { 
      LOG.warn("Bucket {} doesn't exists...Creating one"); 
      s3Client.createBucket(bucketName); 
      LOG.info("Created bucket: {}", bucketName); 
     } 
     bucketUrl = s3Client.getResourceUrl(bucketName, null) + bucketName; 
    } catch (AmazonClientException ace) { 
     LOG.error("An error occurred while connecting to S3. Will not execute action" + 
       " for bucket: {}", bucketName, ace); 
    } 


    return bucketUrl; 
} 
+0

vielen Dank. Haben Sie FileStack.com jemals ausprobiert? –

+0

Nein, ich bin ein Fan von Amazon Web Services :-) Oh, BTW, du hast nach CloudFront gefragt. CloudFront ist ein Content Distribution Network (CDN). Solange Sie also Ihre Dateien in einem S3-Bucket ablegen und dann eine CF-Distribution erstellen, die diesen Bucket verwendet, wird der Inhalt für Ihre Nutzer vom nächstgelegenen Standort an sie geliefert. –

Verwandte Themen