Skip to content

Commit 940d2bd

Browse files
Nick CookShabirmean
authored andcommitted
feat: add Media CDN examples. Move Akamai samples to their own files.
1 parent 1537ff1 commit 940d2bd

File tree

13 files changed

+578
-155
lines changed

13 files changed

+578
-155
lines changed

media/stitcher/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<dependency>
5353
<groupId>com.google.cloud</groupId>
5454
<artifactId>google-cloud-video-stitcher</artifactId>
55-
<version>0.3.4</version>
55+
<version>0.7.0</version>
5656
</dependency>
5757
<dependency>
5858
<groupId>com.google.cloud</groupId>

media/stitcher/src/main/java/com/example/stitcher/CreateCdnKey.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
// [START videostitcher_create_cdn_key]
2020

21-
import com.google.cloud.video.stitcher.v1.AkamaiCdnKey;
2221
import com.google.cloud.video.stitcher.v1.CdnKey;
2322
import com.google.cloud.video.stitcher.v1.CreateCdnKeyRequest;
2423
import com.google.cloud.video.stitcher.v1.GoogleCdnKey;
2524
import com.google.cloud.video.stitcher.v1.LocationName;
25+
import com.google.cloud.video.stitcher.v1.MediaCdnKey;
2626
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
2727
import com.google.protobuf.ByteString;
2828
import java.io.IOException;
@@ -35,39 +35,40 @@ public static void main(String[] args) throws Exception {
3535
String location = "us-central1";
3636
String cdnKeyId = "my-cdn-key-id";
3737
String hostname = "cdn.example.com";
38-
String gcdnKeyname = "my-gcdn-key";
39-
String gcdnPrivateKey = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg"; // will be converted to a byte string
40-
String akamaiTokenKey = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg"; // will be converted to a byte string
38+
String keyName = "my-key";
39+
// To create a privateKey value for Media CDN, see
40+
// https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn.
41+
String privateKey = "my-private-key"; // will be converted to a byte string
42+
Boolean isMediaCdn = true;
4143

42-
createCdnKey(
43-
projectId, location, cdnKeyId, hostname, gcdnKeyname, gcdnPrivateKey, akamaiTokenKey);
44+
createCdnKey(projectId, location, cdnKeyId, hostname, keyName, privateKey, isMediaCdn);
4445
}
4546

46-
// createCdnKey creates an Akamai or Cloud CDN key. If akamaiTokenKey is
47-
// provided, then create an Akamai key. If akamaiTokenKey is not provided (""),
48-
// then create a Cloud CDN key.
47+
// createCdnKey creates a Media CDN key or a Cloud CDN key. A CDN key is used to retrieve
48+
// protected media.
4949
public static void createCdnKey(
5050
String projectId,
5151
String location,
5252
String cdnKeyId,
5353
String hostname,
54-
String gcdnKeyname,
55-
String gcdnPrivateKey,
56-
String akamaiTokenKey)
54+
String keyName,
55+
String privateKey,
56+
Boolean isMediaCdn)
5757
throws IOException {
5858
// Initialize client that will be used to send requests. This client only needs to be created
5959
// once, and can be reused for multiple requests. After completing all of your requests, call
6060
// the "close" method on the client to safely clean up any remaining background resources.
6161
try (VideoStitcherServiceClient videoStitcherServiceClient =
6262
VideoStitcherServiceClient.create()) {
6363
CdnKey cdnKey;
64-
if (akamaiTokenKey != "") {
64+
if (isMediaCdn) {
6565
cdnKey =
6666
CdnKey.newBuilder()
6767
.setHostname(hostname)
68-
.setAkamaiCdnKey(
69-
AkamaiCdnKey.newBuilder()
70-
.setTokenKey(ByteString.copyFromUtf8(akamaiTokenKey))
68+
.setMediaCdnKey(
69+
MediaCdnKey.newBuilder()
70+
.setKeyName(keyName)
71+
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
7172
.build())
7273
.build();
7374
} else {
@@ -76,8 +77,8 @@ public static void createCdnKey(
7677
.setHostname(hostname)
7778
.setGoogleCdnKey(
7879
GoogleCdnKey.newBuilder()
79-
.setKeyName(gcdnKeyname)
80-
.setPrivateKey(ByteString.copyFromUtf8(gcdnPrivateKey))
80+
.setKeyName(keyName)
81+
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
8182
.build())
8283
.build();
8384
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.stitcher;
18+
19+
// [START videostitcher_create_cdn_key_akamai]
20+
21+
import com.google.cloud.video.stitcher.v1.AkamaiCdnKey;
22+
import com.google.cloud.video.stitcher.v1.CdnKey;
23+
import com.google.cloud.video.stitcher.v1.CreateCdnKeyRequest;
24+
import com.google.cloud.video.stitcher.v1.LocationName;
25+
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
26+
import com.google.protobuf.ByteString;
27+
import java.io.IOException;
28+
29+
public class CreateCdnKeyAkamai {
30+
31+
public static void main(String[] args) throws Exception {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "my-project-id";
34+
String location = "us-central1";
35+
String cdnKeyId = "my-cdn-key-id";
36+
String hostname = "cdn.example.com";
37+
String akamaiTokenKey = "my-token-key"; // will be converted to a byte string
38+
39+
createCdnKeyAkamai(projectId, location, cdnKeyId, hostname, akamaiTokenKey);
40+
}
41+
42+
// createCdnKeyAkamai creates an Akamai CDN key. A CDN key is used to retrieve protected media.
43+
public static void createCdnKeyAkamai(
44+
String projectId, String location, String cdnKeyId, String hostname, String akamaiTokenKey)
45+
throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (VideoStitcherServiceClient videoStitcherServiceClient =
50+
VideoStitcherServiceClient.create()) {
51+
CdnKey cdnKey =
52+
CdnKey.newBuilder()
53+
.setHostname(hostname)
54+
.setAkamaiCdnKey(
55+
AkamaiCdnKey.newBuilder()
56+
.setTokenKey(ByteString.copyFromUtf8(akamaiTokenKey))
57+
.build())
58+
.build();
59+
60+
CreateCdnKeyRequest createCdnKeyRequest =
61+
CreateCdnKeyRequest.newBuilder()
62+
.setParent(LocationName.of(projectId, location).toString())
63+
.setCdnKeyId(cdnKeyId)
64+
.setCdnKey(cdnKey)
65+
.build();
66+
67+
CdnKey response = videoStitcherServiceClient.createCdnKey(createCdnKeyRequest);
68+
System.out.println("Created new CDN key: " + response.getName());
69+
}
70+
}
71+
}
72+
// [END videostitcher_create_cdn_key_akamai]

media/stitcher/src/main/java/com/example/stitcher/UpdateCdnKey.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
// [START videostitcher_update_cdn_key]
2020

21-
import com.google.cloud.video.stitcher.v1.AkamaiCdnKey;
2221
import com.google.cloud.video.stitcher.v1.CdnKey;
2322
import com.google.cloud.video.stitcher.v1.CdnKeyName;
2423
import com.google.cloud.video.stitcher.v1.GoogleCdnKey;
24+
import com.google.cloud.video.stitcher.v1.MediaCdnKey;
2525
import com.google.cloud.video.stitcher.v1.UpdateCdnKeyRequest;
2626
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
2727
import com.google.protobuf.ByteString;
@@ -34,27 +34,27 @@ public static void main(String[] args) throws Exception {
3434
// TODO(developer): Replace these variables before running the sample.
3535
String projectId = "my-project-id";
3636
String location = "us-central1";
37-
String cdnKeyId = "my-cdn-key-id2";
37+
String cdnKeyId = "my-updated-cdn-key-id";
3838
String hostname = "updated.example.com";
39-
String gcdnKeyname = "my-gcdn-key";
40-
String gcdnPrivateKey = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg"; // will be converted to a byte string
41-
String akamaiTokenKey = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg"; // will be converted to a byte string
39+
String keyName = "my-key";
40+
// To create a privateKey value for Media CDN, see
41+
// https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn.
42+
String privateKey = "my-updated-private-key"; // will be converted to a byte string
43+
Boolean isMediaCdn = true;
4244

43-
updateCdnKey(
44-
projectId, location, cdnKeyId, hostname, gcdnKeyname, gcdnPrivateKey, akamaiTokenKey);
45+
updateCdnKey(projectId, location, cdnKeyId, hostname, keyName, privateKey, isMediaCdn);
4546
}
4647

47-
// updateCdnKey updates the hostname and key fields for an existing CDN key. If akamaiTokenKey is
48-
// provided, then update the existing Akamai key fields. If akamaiTokenKey is not provided (""),
49-
// then update the existing Cloud CDN key fields. Update hostname regardless of key type.
48+
// updateCdnKey updates the hostname and key fields for an existing Media CDN key or Cloud
49+
// CDN key.
5050
public static void updateCdnKey(
5151
String projectId,
5252
String location,
5353
String cdnKeyId,
5454
String hostname,
55-
String gcdnKeyname,
56-
String gcdnPrivateKey,
57-
String akamaiTokenKey)
55+
String keyName,
56+
String privateKey,
57+
Boolean isMediaCdn)
5858
throws IOException {
5959
// Initialize client that will be used to send requests. This client only needs to be created
6060
// once, and can be reused for multiple requests. After completing all of your requests, call
@@ -63,15 +63,16 @@ public static void updateCdnKey(
6363
VideoStitcherServiceClient.create()) {
6464
CdnKey cdnKey;
6565
String path;
66-
if (akamaiTokenKey != "") {
67-
path = "akamai_cdn_key";
66+
if (isMediaCdn) {
67+
path = "media_cdn_key";
6868
cdnKey =
6969
CdnKey.newBuilder()
7070
.setName(CdnKeyName.of(projectId, location, cdnKeyId).toString())
7171
.setHostname(hostname)
72-
.setAkamaiCdnKey(
73-
AkamaiCdnKey.newBuilder()
74-
.setTokenKey(ByteString.copyFromUtf8(akamaiTokenKey))
72+
.setMediaCdnKey(
73+
MediaCdnKey.newBuilder()
74+
.setKeyName(keyName)
75+
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
7576
.build())
7677
.build();
7778
} else {
@@ -82,17 +83,17 @@ public static void updateCdnKey(
8283
.setHostname(hostname)
8384
.setGoogleCdnKey(
8485
GoogleCdnKey.newBuilder()
85-
.setKeyName(gcdnKeyname)
86-
.setPrivateKey(ByteString.copyFromUtf8(gcdnPrivateKey))
86+
.setKeyName(keyName)
87+
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
8788
.build())
8889
.build();
8990
}
9091

9192
UpdateCdnKeyRequest updateCdnKeyRequest =
9293
UpdateCdnKeyRequest.newBuilder()
9394
.setCdnKey(cdnKey)
94-
// Update the hostname field and the fields for the specific key type (Cloud CDN
95-
// or Akamai). You must set the mask to the fields you want to update.
95+
// Update the hostname field and the fields for the specific key type (Media CDN
96+
// or Cloud CDN). You must set the mask to the fields you want to update.
9697
.setUpdateMask(FieldMask.newBuilder().addPaths("hostname").addPaths(path).build())
9798
.build();
9899

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.stitcher;
18+
19+
// [START videostitcher_update_cdn_key_akamai]
20+
21+
import com.google.cloud.video.stitcher.v1.AkamaiCdnKey;
22+
import com.google.cloud.video.stitcher.v1.CdnKey;
23+
import com.google.cloud.video.stitcher.v1.CdnKeyName;
24+
import com.google.cloud.video.stitcher.v1.UpdateCdnKeyRequest;
25+
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
26+
import com.google.protobuf.ByteString;
27+
import com.google.protobuf.FieldMask;
28+
import java.io.IOException;
29+
30+
public class UpdateCdnKeyAkamai {
31+
32+
public static void main(String[] args) throws Exception {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "my-project-id";
35+
String location = "us-central1";
36+
String cdnKeyId = "my-updated-cdn-key-id";
37+
String hostname = "updated.example.com";
38+
String akamaiTokenKey = "my-updated-token-key"; // will be converted to a byte string
39+
40+
updateCdnKeyAkamai(projectId, location, cdnKeyId, hostname, akamaiTokenKey);
41+
}
42+
43+
// updateCdnKeyAkamai updates the hostname and key fields for an existing CDN key.
44+
public static void updateCdnKeyAkamai(
45+
String projectId, String location, String cdnKeyId, String hostname, String akamaiTokenKey)
46+
throws IOException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests. After completing all of your requests, call
49+
// the "close" method on the client to safely clean up any remaining background resources.
50+
try (VideoStitcherServiceClient videoStitcherServiceClient =
51+
VideoStitcherServiceClient.create()) {
52+
CdnKey cdnKey =
53+
CdnKey.newBuilder()
54+
.setName(CdnKeyName.of(projectId, location, cdnKeyId).toString())
55+
.setHostname(hostname)
56+
.setAkamaiCdnKey(
57+
AkamaiCdnKey.newBuilder()
58+
.setTokenKey(ByteString.copyFromUtf8(akamaiTokenKey))
59+
.build())
60+
.build();
61+
62+
UpdateCdnKeyRequest updateCdnKeyRequest =
63+
UpdateCdnKeyRequest.newBuilder()
64+
.setCdnKey(cdnKey)
65+
// Update the hostname field and token key field. You must set the mask to the fields
66+
// you want to update.
67+
.setUpdateMask(
68+
FieldMask.newBuilder().addPaths("hostname").addPaths("akamai_cdn_key").build())
69+
.build();
70+
71+
CdnKey response = videoStitcherServiceClient.updateCdnKey(updateCdnKeyRequest);
72+
System.out.println("Updated CDN key: " + response.getName());
73+
}
74+
}
75+
}
76+
// [END videostitcher_update_cdn_key_akamai]

0 commit comments

Comments
 (0)