/
resource_tc_cos_object_download_operation.go
87 lines (72 loc) · 2.44 KB
/
resource_tc_cos_object_download_operation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cos
import (
"context"
"io"
"log"
"os"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceTencentCloudCosObjectDownloadOperation() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudCosObjectDownloadOperationCreate,
Read: resourceTencentCloudCosObjectDownloadOperationRead,
Delete: resourceTencentCloudCosObjectDownloadOperationDelete,
Schema: map[string]*schema.Schema{
"bucket": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Bucket.",
},
"key": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Object key.",
},
"download_path": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Download path.",
},
},
}
}
func resourceTencentCloudCosObjectDownloadOperationCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cos_object_download_operation.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
bucket := d.Get("bucket").(string)
key := d.Get("key").(string)
downloadPath := d.Get("download_path").(string)
resp, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClient(bucket).Object.Get(ctx, key, nil)
if err != nil {
log.Printf("[CRITAL]%s object download failed, reason:%+v", logId, err)
return err
}
defer resp.Body.Close()
fd, err := os.OpenFile(downloadPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
return err
}
_, err = io.Copy(fd, resp.Body)
fd.Close()
if err != nil {
return err
}
d.SetId(bucket + tccommon.FILED_SP + key)
return resourceTencentCloudCosObjectDownloadOperationRead(d, meta)
}
func resourceTencentCloudCosObjectDownloadOperationRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cos_object_download_operation.read")()
defer tccommon.InconsistentCheck(d, meta)()
return nil
}
func resourceTencentCloudCosObjectDownloadOperationDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cos_object_download_operation.delete")()
defer tccommon.InconsistentCheck(d, meta)()
return nil
}