S3 Browser介绍、基础操作

一、S3 Browser 8-1-15 简介

S3 Browser 8-1-15是Amazon S3的客户端应用程序,用于管理和操作Amazon S3存储桶和对象。

二、安装包

下载地址:百度网盘 请输入提取码

提取码:9acn

三、基础操作

1)连接客户端

1、通过S3 Browser连接上客户端。

2、Account Name暂时不知道作用,应该是创建account时候固定的名称。

这里我用的已经创建的参数进行连接。

S3 Browser介绍、基础操作_第1张图片

 

2)上传、下载

1、现在代码里用的桶是itpms的,路径需要自己创建。

S3 Browser介绍、基础操作_第2张图片

2、创建路径

S3 Browser介绍、基础操作_第3张图片

 支持链式创建目录:

S3 Browser介绍、基础操作_第4张图片

3、上传文件

可以可视化界面上传,也可以直接拖拽文件到界面。

S3 Browser介绍、基础操作_第5张图片

 4、下载文件

选中文件,点击Download

S3 Browser介绍、基础操作_第6张图片

 

JAVA代码-上传下载

private boolean s3Enable = false;

@PostConstruct
    private synchronized void init() throws AppException {
        s3Enable = Boolean.valueOf(ConfigUtil.getConfigFromPropertiesFirst("CONFIG_S3_ENABLE", "true"));

        if (s3Enable) {
            SysParameter accessKeyParamter = "S3_ACCESSKEY";
            SysParameter secretKeyParamter = "S3_SECRETKEY";
            SysParameter bucketNameParamter = "S3_BUCKETNAME";
            SysParameter endpointParamter = "S3_ENDPOINT";
            SysParameter bufferSizeParamter = "S3_BUFFER");
            SysParameter archiveWsdlLocationParamter = "CONFIG_ARCHIVE_WSDL_LOCATION";
            SysParameter s3EnableParameter = "CONFIG_S3_ENABLE";
            checkSysParameter(accessKeyParamter, secretKeyParamter, bucketNameParamter, endpointParamter,
                bufferSizeParamter, archiveWsdlLocationParamter, s3EnableParameter);

            accessKey = accessKeyParamter.getPvalue();
            secretKey = secretKeyParamter.getPvalue();
            bucketName = bucketNameParamter.getPvalue();
            endpoint = endpointParamter.getPvalue();
            bufferSize = Integer.valueOf(bufferSizeParamter.getPvalue());
            archiveWsdlLocation = archiveWsdlLocationParamter.getPvalue();
            s3Enable = Boolean.valueOf(s3EnableParameter.getPvalue());

            AmazonS3 conn = getS3();
            if (!conn.doesBucketExist(bucketName)) {
                LOGGER.info("Bucket name:[{}] 不存在,新建该Bucket", bucketName);
                conn.createBucket(bucketName);
            }
            LOGGER.info("初始化S3客户端完成,Endpoint:[{}], Bucket name:[{}],", endpoint, bucketName);
        } else {
            LOGGER.info("不启用S3客户端。");
        }

    }


private AmazonS3 getS3() {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTP);
        EndpointConfiguration endpointConfiguration = new EndpointConfiguration(endpoint, Regions.CN_NORTH_1.getName());

        AmazonS3 conn = AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)
            .withClientConfiguration(clientConfig).withCredentials(new AWSStaticCredentialsProvider(credentials))
            // 是否使用路径方式
            .withPathStyleAccessEnabled(Boolean.TRUE)
            .build();
        return conn;
    }


/**
     * 文件下载
     *
     * @param path
     * @param response
     * @throws AppException
     */
    @Override
    public void fileDownload(String path, HttpServletResponse response) throws AppException {
        // path是指欲下载的文件的路径。
        File file = new File(path);
        String fileName = file.getName();

        // 取得文件名。
        LOGGER.info("下载文件名:{}", fileName);
        LOGGER.info("下载S3文件路径:{}", path);

        // 如果本地有存,无需下载
        if (file.exists()) {
            try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
                downloadFile(response, fileName, is);
            } catch (Exception e) {
                throw new AppException("Download file err. ", e);
            }
        } else {
            if (s3Enable) {
                path = formatS3Filename(path);

                LOGGER.info("format之后S3文件路径:{}", path);
                AmazonS3 conn = getS3();

                try {

                    S3Object s3Object = conn.getObject(new GetObjectRequest(bucketName, path));
                    if (s3Object != null) {
                        LOGGER.debug("s3_content-type : {}", s3Object.getObjectMetadata().getContentType());
                        LOGGER.debug("s3_etag : {}", s3Object.getObjectMetadata().getETag());
                        LOGGER.debug("s3_content_length : {}", s3Object.getObjectMetadata().getContentLength());

                        // 创建文件夹和文件
                        FileUtils.createFolderAndFile(file);
                        try (InputStream is = s3Object.getObjectContent();
                            OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
                            IOUtils.copy(is, os);
                        } catch (IOException e) {
                            LOGGER.error("下载文件报错", e);
                            throw new AppException("下载文件报错", e);
                        }

                        if (file.exists()) {
                            try {
                                InputStream downloadFileIs = new FileInputStream(file);
                                downloadFile(response, fileName, downloadFileIs);
                            } catch (FileNotFoundException e) {
                                throw new AppException("下载文件报错", e);
                            }
                        }
                    }
                } catch (SdkClientException | IOException e) {
                    throw new AppException("下载文件失败", e);
                }
            }

        }
    }

private String formatS3Filename(String path) {
        if (path == null) {
            return null;
        }
        path = FilenameUtils.separatorsToUnix(path);
        path = path.startsWith("/") ? path.substring(1) : path;
        return path;
    }


private void downloadFile(HttpServletResponse response, String fileName, InputStream is) throws AppException {
        try (InputStream inputStream = new BufferedInputStream(is);
            OutputStream outputStream = response.getOutputStream()) {
            fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("UTF-8");

            byte[] buffer = new byte[bufferSize];
            int length = -1;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

        } catch (IOException e) {
            LOGGER.error("Download file from s3 err. ", e);
            throw new AppException("下载文件失败", e);
        }
    }



/**
     * 文件上传
     *
     * @param request
     * @throws IOException
     * @throws IllegalStateException
     */
    @Override
    public File fileUpload(String uploadDir, HttpServletRequest request) throws AppException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        MultipartFile file = multipartRequest.getFile("file");
        if (file == null) {
            return null;
        }
        try {
            File targetFile = FileDownAndUpload.filedUpload(uploadDir, multipartRequest);
            String bigRealFilePath = targetFile.getPath();
            if (s3Enable) {
                return fileSaveByPath(bigRealFilePath, targetFile);
            } else {
                return targetFile;
            }
        } catch (Exception e) {
            LOGGER.error("上传文件失败", e);
            throw new AppException("上传文件失败", e);
        }

    }



 /**
     * 文件上传,仅支持服务器本地存储,不支持分布式存储,在分布式环境下可能找不到文件。
     *
     * @param request
     * @throws IOException
     * @throws IllegalStateException
     */
    @Deprecated
    public static File filedUpload(String uploadDir, HttpServletRequest request)
        throws AppException, IllegalStateException, IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        MultipartFile file = multipartRequest.getFile("file");
        // file= new String(file.getBytes("ISO-8859-1 ","UTF-8"));
        if (file == null) {// step-2 判断file
            return null;
        }
        String orgFileName = file.getOriginalFilename();
        orgFileName = (orgFileName == null) ? "" : orgFileName;
        Pattern p = Pattern.compile("\\s|\t|\r|\n");
        Matcher m = p.matcher(orgFileName);
        orgFileName = m.replaceAll("_");
        if (!(new File(uploadDir).exists())) {
            new File(uploadDir).mkdirs();
        }
        String sep = System.getProperty("file.separator");

        String timestring = DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS");

        String bigRealFilePath =
            uploadDir + (uploadDir.endsWith(sep) ? "" : sep) + timestring + "_" + file.getOriginalFilename();

        logger.info("上传文件路径:" + bigRealFilePath);

        // 文件非空判断
        /* if (file.getSize() > 0) { */
        File targetFile = new File(bigRealFilePath);

        file.transferTo(targetFile);// 写入目标文件

        // 重命名
        // File newfile = ReName(targetFile);

        return targetFile;
    }

你可能感兴趣的:(java,开发语言)