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

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

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

问题

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

import java.io.File;
import java.io.IOException;

public class UploadObject {

  public static void main(String[] args) throws IOException {

    Regions clientRegion = Regions.US_EAST_1;

    String fileObjKeyName = "N.pdf";

    String fileName = "C:\\home\\aws\\N.pdf";

    //To Test the File Upload

    String accessKeyId = "AKIAZGSMNGVXXXZ73VXE";

    String secretAccessKey = "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";

    String bucketName = "fioprod-s3-addon-us-east-12";

    try {

      final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);

      //This code expects that you have AWS credentials set up per:
      // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html

      AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
          .withRegion(clientRegion)
          .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
          .build();

      // Upload a file as a new object with ContentType and title specified.

      PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));

      ObjectMetadata metadata = new ObjectMetadata();

      metadata.setContentType("plain/text");

      metadata.addUserMetadata("title", "someTitle");

      request.setMetadata(metadata);

      s3Client.putObject(request);

    } catch (AmazonServiceException e) {

      // The call was transmitted successfully, but Amazon S3 couldn't process
      // it, so it returned an error response.

      e.printStackTrace();

    } catch (SdkClientException e) {

      // Amazon S3 couldn't be contacted for a response, or the client
      // couldn't parse the response from Amazon S3.

      e.printStackTrace();

    }

  }

}

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.


<details>
<summary>英文:</summary>
```java
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.File;
import java.io.IOException;
public class UploadObject {
public static void main(String[] args) throws IOException {
Regions clientRegion = Regions.US_EAST_1;
String fileObjKeyName = &quot;N.pdf&quot;;
String fileName = &quot;C:\\home\\aws\\N.pdf&quot;;
//To Test the File Upload
String accessKeyId = &quot;AKIAZGSMNGVXXXZ73VXE&quot;;
String secretAccessKey = &quot;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&quot;;
String bucketName = &quot;fioprod-s3-addon-us-east-12&quot;;
try {
final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
//This code expects that you have AWS credentials set up per:
// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.build();
// Upload a file as a new object with ContentType and title specified.
PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(&quot;plain/text&quot;);
metadata.addUserMetadata(&quot;title&quot;, &quot;someTitle&quot;);
request.setMetadata(metadata);
s3Client.putObject(request);
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn&#39;t process
// it, so it returned an error response.
e.printStackTrace();
} catch (SdkClientException e) {
// Amazon S3 couldn&#39;t be contacted for a response, or the client
// couldn&#39;t parse the response from Amazon S3.
e.printStackTrace();
}
}
}

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中包含了您的代码并进行了一些小的更改,使您的代码可以工作。

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

import java.io.File;
import java.io.IOException;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException{
	Object[] r = getRow();
 
 if (r == null) {
		setOutputDone();
		return false;
	}

    Regions clientRegion = Regions.US_EAST_1;

    String fileObjKeyName = "N.pdf";

    String fileName = "C:\\home\\aws\\N.pdf";

    //To Test the File Upload

    String accessKeyId = "AKIAZGSMNGVXXXZ73VXE";

    String secretAccessKey = "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";

    String bucketName = "fioprod-s3-addon-us-east-12";
    

      final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);

      //This code expects that you have AWS credentials set up per:

      // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html

      AmazonS3 s3Client = (AmazonS3)AmazonS3ClientBuilder.standard().withRegion(clientRegion).withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)).build();

      // Upload a file as a new object with ContentType and title specified.

      PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));

      ObjectMetadata metadata = new ObjectMetadata();

      metadata.setContentType("plain/text");

      metadata.addUserMetadata("title", "someTitle");

      request.setMetadata(metadata);

      s3Client.putObject(request);

   
	putRow(data.outputRowMeta, r);
 	 return true;

}
英文:

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

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.File;
import java.io.IOException;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException{
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
Regions clientRegion = Regions.US_EAST_1;
String fileObjKeyName = &quot;N.pdf&quot;;
String fileName = &quot;C:\\home\\aws\\N.pdf&quot;;
//To Test the File Upload
String accessKeyId = &quot;AKIAZGSMNGVXXXZ73VXE&quot;;
String secretAccessKey = &quot;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&quot;;
String bucketName = &quot;fioprod-s3-addon-us-east-12&quot;;
final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
//This code expects that you have AWS credentials set up per:
// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html
AmazonS3 s3Client = (AmazonS3)AmazonS3ClientBuilder.standard().withRegion(clientRegion).withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)).build();
// Upload a file as a new object with ContentType and title specified.
PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(&quot;plain/text&quot;);
metadata.addUserMetadata(&quot;title&quot;, &quot;someTitle&quot;);
request.setMetadata(metadata);
s3Client.putObject(request);
putRow(data.outputRowMeta, r);
return true;
}
</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:

确定