Skip to content

Commit fcd06ac

Browse files
authored
Add UploadMediaBundle.java (#280)
1 parent 3282a23 commit fcd06ac

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//
2+
// Copyright 2020 Google LLC
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.ads.googleads.examples.misc;
16+
17+
import com.beust.jcommander.Parameter;
18+
import com.google.ads.googleads.examples.utils.ArgumentNames;
19+
import com.google.ads.googleads.examples.utils.CodeSampleParams;
20+
import com.google.ads.googleads.lib.GoogleAdsClient;
21+
import com.google.ads.googleads.v3.enums.MediaTypeEnum.MediaType;
22+
import com.google.ads.googleads.v3.errors.GoogleAdsError;
23+
import com.google.ads.googleads.v3.errors.GoogleAdsException;
24+
import com.google.ads.googleads.v3.resources.MediaBundle;
25+
import com.google.ads.googleads.v3.resources.MediaFile;
26+
import com.google.ads.googleads.v3.services.MediaFileOperation;
27+
import com.google.ads.googleads.v3.services.MediaFileServiceClient;
28+
import com.google.ads.googleads.v3.services.MutateMediaFilesResponse;
29+
import com.google.common.io.ByteStreams;
30+
import com.google.protobuf.ByteString;
31+
import com.google.protobuf.BytesValue;
32+
import com.google.protobuf.StringValue;
33+
import java.io.FileNotFoundException;
34+
import java.io.IOException;
35+
import java.net.URL;
36+
import java.util.Arrays;
37+
38+
/**
39+
* Uploads an HTML5 zip file as a media bundle. More information about media bundles can be found at
40+
* https://developers.google.com/google-ads/api/docs/assets/overview.
41+
*/
42+
public class UploadMediaBundle {
43+
44+
private static final String BUNDLE_URL = "https://goo.gl/9Y7qI2";
45+
46+
private static class UploadMediaBundleParams extends CodeSampleParams {
47+
48+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
49+
private Long customerId;
50+
}
51+
52+
public static void main(String[] args) throws IOException {
53+
UploadMediaBundleParams params = new UploadMediaBundleParams();
54+
if (!params.parseArguments(args)) {
55+
56+
// Either pass the required parameters for this example on the command line, or insert them
57+
// into the code here. See the parameter class definition above for descriptions.
58+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
59+
}
60+
61+
GoogleAdsClient googleAdsClient;
62+
try {
63+
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
64+
} catch (FileNotFoundException fnfe) {
65+
System.err.printf(
66+
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
67+
return;
68+
} catch (IOException ioe) {
69+
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
70+
return;
71+
}
72+
73+
try {
74+
new UploadMediaBundle().runExample(googleAdsClient, params.customerId);
75+
} catch (GoogleAdsException gae) {
76+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
77+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
78+
// collection of GoogleAdsErrors that indicate the underlying causes of the
79+
// GoogleAdsException.
80+
System.err.printf(
81+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
82+
gae.getRequestId());
83+
int i = 0;
84+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
85+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
86+
}
87+
}
88+
}
89+
90+
/**
91+
* Runs the example.
92+
*
93+
* @param googleAdsClient the Google Ads API client.
94+
* @param customerId the client customer ID.
95+
* @throws GoogleAdsException if an API request failed with one or more service errors.
96+
*/
97+
private void runExample(GoogleAdsClient googleAdsClient, long customerId) throws IOException {
98+
99+
// Reads the sample media bundle from the URL into a byte array.
100+
byte[] bundleData = ByteStreams.toByteArray(new URL(BUNDLE_URL).openStream());
101+
102+
// Creates a media bundle file.
103+
MediaBundle bundle =
104+
MediaBundle.newBuilder().setData(BytesValue.of(ByteString.copyFrom(bundleData))).build();
105+
106+
// Creates a media file.
107+
MediaFile file =
108+
MediaFile.newBuilder()
109+
.setName(StringValue.of("Ad Media Bundle"))
110+
.setType(MediaType.MEDIA_BUNDLE)
111+
.setSourceUrl(StringValue.of(BUNDLE_URL))
112+
.setMediaBundle(bundle)
113+
.build();
114+
115+
// Creates a media file operation.
116+
MediaFileOperation op = MediaFileOperation.newBuilder().setCreate(file).build();
117+
118+
// Issues a mutate request to add the media file.
119+
try (MediaFileServiceClient mediaFileServiceClient =
120+
googleAdsClient.getLatestVersion().createMediaFileServiceClient()) {
121+
MutateMediaFilesResponse response =
122+
mediaFileServiceClient.mutateMediaFiles(Long.toString(customerId), Arrays.asList(op));
123+
System.out.printf(
124+
"The media bundle with resource name '%s' was added.%n",
125+
response.getResults(0).getResourceName());
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)