0

モバイル アプリケーション (フロントエンド) から動画や画像をアップロードするサービスを探しています。Amazon S3 と CloudFront について聞いたことがあります。それらを保存し、特定の基準 (たとえば、写真あたりの最大ファイル サイズは 3 MB など) を満たしているかどうかを確認し、ファイルが満たされていない場合はクライアントにエラーを返すサービスを探しています。基準。Amazon S3 または CloudFront はこれを提供しますか? そうでない場合、他に推奨されるサービスはありますか?

4

1 に答える 1

2

AWS SDK を使用できます。Java バージョンの例を次に示します (Amazon はさまざまな言語用の SDK を提供しています)。

/**
 * 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;

}

画像を保存する前にバケットが存在するかどうかを確認するなど、他の操作を実行することもできます

/**
 * 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;
}
于 2016-04-18T19:48:25.233 に答える