将PDF文件通过用户定义的Java类加载到Pentaho中的S3。

huangapple go评论88阅读模式
英文:

Load a pdf into S3 using User Defined Java Class in Pentaho

问题

  1. import com.amazonaws.AmazonServiceException;
  2. import com.amazonaws.SdkClientException;
  3. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  4. import com.amazonaws.auth.BasicAWSCredentials;
  5. import com.amazonaws.regions.Regions;
  6. import com.amazonaws.services.s3.AmazonS3;
  7. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  8. import com.amazonaws.services.s3.model.ObjectMetadata;
  9. import com.amazonaws.services.s3.model.PutObjectRequest;
  10. import java.io.File;
  11. import java.io.IOException;
  12. public class UploadObject {
  13. public static void main(String[] args) throws IOException {
  14. Regions clientRegion = Regions.US_EAST_1;
  15. String fileObjKeyName = "N.pdf";
  16. String fileName = "C:\\home\\aws\\N.pdf";
  17. //To Test the File Upload
  18. String accessKeyId = "AKIAZGSMNGVXXXZ73VXE";
  19. String secretAccessKey = "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";
  20. String bucketName = "fioprod-s3-addon-us-east-12";
  21. try {
  22. final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
  23. //This code expects that you have AWS credentials set up per:
  24. // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
  25. AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  26. .withRegion(clientRegion)
  27. .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
  28. .build();
  29. // Upload a file as a new object with ContentType and title specified.
  30. PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
  31. ObjectMetadata metadata = new ObjectMetadata();
  32. metadata.setContentType("plain/text");
  33. metadata.addUserMetadata("title", "someTitle");
  34. request.setMetadata(metadata);
  35. s3Client.putObject(request);
  36. } catch (AmazonServiceException e) {
  37. // The call was transmitted successfully, but Amazon S3 couldn't process
  38. // it, so it returned an error response.
  39. e.printStackTrace();
  40. } catch (SdkClientException e) {
  41. // Amazon S3 couldn't be contacted for a response, or the client
  42. // couldn't parse the response from Amazon S3.
  43. e.printStackTrace();
  44. }
  45. }
  46. }

The above code works fine to load a PDF file into S3 when I run it from IntelliJ IDE. I want this code to move to Pentaho's "User Defined Class". However, when I do that, it throws an error - "Imported class 'com.amazonaws.auth.AWSStaticCredentialsProvider' could not be loaded".

How do I resolve that? My ultimate goal is to load a .pdf or .zip file into S3 using Pentaho.

Thank you for your time.

  1. <details>
  2. <summary>英文:</summary>
  3. ```java
  4. import com.amazonaws.AmazonServiceException;
  5. import com.amazonaws.SdkClientException;
  6. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  7. import com.amazonaws.auth.BasicAWSCredentials;
  8. import com.amazonaws.regions.Regions;
  9. import com.amazonaws.services.s3.AmazonS3;
  10. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  11. import com.amazonaws.services.s3.model.ObjectMetadata;
  12. import com.amazonaws.services.s3.model.PutObjectRequest;
  13. import java.io.File;
  14. import java.io.IOException;
  15. public class UploadObject {
  16. public static void main(String[] args) throws IOException {
  17. Regions clientRegion = Regions.US_EAST_1;
  18. String fileObjKeyName = &quot;N.pdf&quot;;
  19. String fileName = &quot;C:\\home\\aws\\N.pdf&quot;;
  20. //To Test the File Upload
  21. String accessKeyId = &quot;AKIAZGSMNGVXXXZ73VXE&quot;;
  22. String secretAccessKey = &quot;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&quot;;
  23. String bucketName = &quot;fioprod-s3-addon-us-east-12&quot;;
  24. try {
  25. final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
  26. //This code expects that you have AWS credentials set up per:
  27. // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
  28. AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  29. .withRegion(clientRegion)
  30. .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
  31. .build();
  32. // Upload a file as a new object with ContentType and title specified.
  33. PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
  34. ObjectMetadata metadata = new ObjectMetadata();
  35. metadata.setContentType(&quot;plain/text&quot;);
  36. metadata.addUserMetadata(&quot;title&quot;, &quot;someTitle&quot;);
  37. request.setMetadata(metadata);
  38. s3Client.putObject(request);
  39. } catch (AmazonServiceException e) {
  40. // The call was transmitted successfully, but Amazon S3 couldn&#39;t process
  41. // it, so it returned an error response.
  42. e.printStackTrace();
  43. } catch (SdkClientException e) {
  44. // Amazon S3 couldn&#39;t be contacted for a response, or the client
  45. // couldn&#39;t parse the response from Amazon S3.
  46. e.printStackTrace();
  47. }
  48. }
  49. }

The above code works fine to load pdf file into S3 when I run it from IntelliJ IDE. I want this code to move to Pentaho "User Defined Class", when I do that it throws error - " Imported class "com.amazonaws.auth.AWSStaticCredentialsProvider" could not be loaded"

How do I resolve that? My ultimate goal is to load a .pdf or .zip file into S3 using pentaho.

Thank you for your time.

答案1

得分: 1

你已经编写了很好的代码,而且它也正常工作。您只需要将aws-java-sdk jar文件放到您的data-integration/lib位置。

您可以从这里下载sdk jar文件。

您还可以从这里查看我的KTR,我在PDI的User-defined-java-class中包含了您的代码并进行了一些小的更改,使您的代码可以工作。

  1. import com.amazonaws.AmazonServiceException;
  2. import com.amazonaws.SdkClientException;
  3. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  4. import com.amazonaws.auth.BasicAWSCredentials;
  5. import com.amazonaws.regions.Regions;
  6. import com.amazonaws.services.s3.AmazonS3;
  7. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  8. import com.amazonaws.services.s3.model.ObjectMetadata;
  9. import com.amazonaws.services.s3.model.PutObjectRequest;
  10. import java.io.File;
  11. import java.io.IOException;
  12. public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException{
  13. Object[] r = getRow();
  14. if (r == null) {
  15. setOutputDone();
  16. return false;
  17. }
  18. Regions clientRegion = Regions.US_EAST_1;
  19. String fileObjKeyName = "N.pdf";
  20. String fileName = "C:\\home\\aws\\N.pdf";
  21. //To Test the File Upload
  22. String accessKeyId = "AKIAZGSMNGVXXXZ73VXE";
  23. String secretAccessKey = "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";
  24. String bucketName = "fioprod-s3-addon-us-east-12";
  25. final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
  26. //This code expects that you have AWS credentials set up per:
  27. // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
  28. AmazonS3 s3Client = (AmazonS3)AmazonS3ClientBuilder.standard().withRegion(clientRegion).withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)).build();
  29. // Upload a file as a new object with ContentType and title specified.
  30. PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
  31. ObjectMetadata metadata = new ObjectMetadata();
  32. metadata.setContentType("plain/text");
  33. metadata.addUserMetadata("title", "someTitle");
  34. request.setMetadata(metadata);
  35. s3Client.putObject(request);
  36. putRow(data.outputRowMeta, r);
  37. return true;
  38. }
英文:

Your have written nice code which is working as well. You just need to keep aws-java-sdk jar to your data-integration/lib location.

You can download sdk jar file from Here

You can look my KTR also from Here where I have included your code and make small changes to workable your code in User-defined-java-class in PDI

  1. import com.amazonaws.AmazonServiceException;
  2. import com.amazonaws.SdkClientException;
  3. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  4. import com.amazonaws.auth.BasicAWSCredentials;
  5. import com.amazonaws.regions.Regions;
  6. import com.amazonaws.services.s3.AmazonS3;
  7. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  8. import com.amazonaws.services.s3.model.ObjectMetadata;
  9. import com.amazonaws.services.s3.model.PutObjectRequest;
  10. import java.io.File;
  11. import java.io.IOException;
  12. public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException{
  13. Object[] r = getRow();
  14. if (r == null) {
  15. setOutputDone();
  16. return false;
  17. }
  18. Regions clientRegion = Regions.US_EAST_1;
  19. String fileObjKeyName = &quot;N.pdf&quot;;
  20. String fileName = &quot;C:\\home\\aws\\N.pdf&quot;;
  21. //To Test the File Upload
  22. String accessKeyId = &quot;AKIAZGSMNGVXXXZ73VXE&quot;;
  23. String secretAccessKey = &quot;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&quot;;
  24. String bucketName = &quot;fioprod-s3-addon-us-east-12&quot;;
  25. final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
  26. //This code expects that you have AWS credentials set up per:
  27. // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
  28. AmazonS3 s3Client = (AmazonS3)AmazonS3ClientBuilder.standard().withRegion(clientRegion).withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)).build();
  29. // Upload a file as a new object with ContentType and title specified.
  30. PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
  31. ObjectMetadata metadata = new ObjectMetadata();
  32. metadata.setContentType(&quot;plain/text&quot;);
  33. metadata.addUserMetadata(&quot;title&quot;, &quot;someTitle&quot;);
  34. request.setMetadata(metadata);
  35. s3Client.putObject(request);
  36. putRow(data.outputRowMeta, r);
  37. return true;
  38. }
  39. </details>

huangapple
  • 本文由 发表于 2020年9月26日 01:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定