Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to upload on any cloud? #8

Closed
ypkkhatri opened this issue Sep 19, 2019 · 2 comments
Closed

How to upload on any cloud? #8

ypkkhatri opened this issue Sep 19, 2019 · 2 comments

Comments

@ypkkhatri
Copy link

ypkkhatri commented Sep 19, 2019

Can you please show the information or code to upload on any cloud? Because in your code its not mentioned.

@SeunMatt
Copy link
Owner

Hello @ypkkhatri

Here's an example snippet from one of my projects.

public boolean backup() {

    Properties properties = new Properties();
    properties.setProperty(MysqlExportService.JDBC_CONNECTION_STRING, dataSourceProperties.getUrl());
    properties.setProperty(MysqlExportService.DB_USERNAME, dataSourceProperties.getUsername());
    properties.setProperty(MysqlExportService.DB_PASSWORD, dataSourceProperties.getPassword());

    properties.setProperty(MysqlExportService.PRESERVE_GENERATED_ZIP, "true");
    properties.setProperty(MysqlExportService.ADD_IF_NOT_EXISTS, "true");

    String basePath = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
    File tempFile = new File(basePath, "/backup");

    properties.setProperty(MysqlExportService.TEMP_DIR, tempFile.getPath());
    MysqlExportService mySqlExportService = new MysqlExportService(properties);
    try {
        mySqlExportService.export();
        storageService.storeBackupFile(mySqlExportService.getGeneratedZipFile());
        return true;
    } catch (Exception e) {
        logger.error("Error Occurred in DatabaseBackupService: " + e.getLocalizedMessage());
        e.printStackTrace();
        return false;
    } finally {
        mySqlExportService.clearTempFiles(false);
    }
}

I configured the properties PRESERVE_GENERATED_ZIP to true.
What this means is that when I call the mySqlExportService.export(); method,
the generated zip file will be preserved and I can access it as java.io.File object.

Which is exactly what I did here storageService.storeBackupFile(mySqlExportService.getGeneratedZipFile());

In my case, the StorageService makes use of the AWS S3 SDK to upload the generated zip file to an S3 bucket.

Here is the implementation of that storeBackupFile() method:

public void storeBackupFile(File file) {

    if(!Objects.isNull(file)) {
        String newFileName = RandomStringUtils.randomAlphanumeric(5) + "_" + file.getName();
        AmazonS3 s3 = s3Factory();
        String bucketName = storageProperties.getAwsS3BucketName() + "/backup";

        Try.of(() -> s3.putObject(
                new PutObjectRequest(bucketName, newFileName, new FileInputStream(file), null)
                        .withCannedAcl(CannedAccessControlList.PublicRead)))
                .onFailure(e -> logger.error("Error Storing Object to S3: " + e.getLocalizedMessage())).get();
    }
}

I hope this helps you.

Cheers

@ypkkhatri
Copy link
Author

@SeunMatt Thanks, for example,.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants