【问题标题】:Service to upload images and videos to (S3, CloudFront etc)将图像和视频上传到(S3、CloudFront 等)的服务
【发布时间】:2016-04-19 03:51:43
【问题描述】:

我正在寻找从我的移动应用程序(前端)上传视频和图像的服务。我听说过 Amazon S3 和 CloudFront。我正在寻找一种可以存储它们的服务,并且还能够检查它们是否满足某些条件(例如,每张图片的最大文件大小为 3MB),如果文件不满足,则向客户端返回错误标准。 Amazon S3 或 CloudFront 是否提供此功能?如果没有,还有其他推荐的服务吗?

【问题讨论】:

    标签: amazon-web-services amazon-s3 cloud cloud-storage


    【解决方案1】:

    您可以使用 AWS 开发工具包。下面是 Java 版本的示例(亚马逊提供了不同语言的 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;
    }
    

    【讨论】:

    • 不,我是 Amazon Web Services 的粉丝 :-) 哦,顺便说一句,您问的是 CloudFront。 CloudFront 是一个内容分发网络 (CDN)。因此,只要您将文件放入 S3 存储桶,然后创建使用该存储桶的 CF 分发,内容就会从离他们最近的位置提供给您的用户。
    猜你喜欢
    • 1970-01-01
    • 2011-06-16
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2015-12-20
    • 2014-09-01
    相关资源
    最近更新 更多