github.com/cyverse/go-irodsclient@v0.13.2/examples/upload_parallel/upload_parallel.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/cyverse/go-irodsclient/fs"
    10  	"github.com/cyverse/go-irodsclient/irods/types"
    11  	"github.com/cyverse/go-irodsclient/irods/util"
    12  
    13  	log "github.com/sirupsen/logrus"
    14  )
    15  
    16  func main() {
    17  	logger := log.WithFields(log.Fields{
    18  		"package":  "main",
    19  		"function": "main",
    20  	})
    21  
    22  	// Parse cli parameters
    23  	flag.Parse()
    24  	args := flag.Args()
    25  
    26  	if len(args) != 2 {
    27  		fmt.Fprintf(os.Stderr, "Give a local source path and an iRODS destination path!\n")
    28  		os.Exit(1)
    29  	}
    30  
    31  	srcPath := args[0]
    32  	destPath := args[1]
    33  
    34  	// Read account configuration from YAML file
    35  	yaml, err := os.ReadFile("account.yml")
    36  	if err != nil {
    37  		logger.Error(err)
    38  		panic(err)
    39  	}
    40  
    41  	account, err := types.CreateIRODSAccountFromYAML(yaml)
    42  	if err != nil {
    43  		logger.Error(err)
    44  		panic(err)
    45  	}
    46  
    47  	logger.Debugf("Account : %v", account.MaskSensitiveData())
    48  
    49  	// Create a file system
    50  	appName := "upload"
    51  	filesystem, err := fs.NewFileSystemWithDefault(account, appName)
    52  	if err != nil {
    53  		logger.Error(err)
    54  		panic(err)
    55  	}
    56  
    57  	defer filesystem.Release()
    58  
    59  	// convert src path into absolute path
    60  	srcPath, err = filepath.Abs(srcPath)
    61  	if err != nil {
    62  		logger.Error(err)
    63  		panic(err)
    64  	}
    65  
    66  	err = filesystem.UploadFileParallel(srcPath, destPath, "", 0, false, nil)
    67  	if err != nil {
    68  		logger.Error(err)
    69  		panic(err)
    70  	}
    71  
    72  	fsentry, err := filesystem.Stat(destPath)
    73  	if err != nil {
    74  		logger.Error(err)
    75  		panic(err)
    76  	}
    77  
    78  	if fsentry.Type == fs.FileEntry {
    79  		fmt.Printf("Successfully uploaded a file %s to %s, size = %d\n", srcPath, destPath, fsentry.Size)
    80  	} else {
    81  		// dir
    82  		srcFileName := util.GetIRODSPathFileName(srcPath)
    83  		destFilePath := util.MakeIRODSPath(destPath, srcFileName)
    84  
    85  		fsentry2, err := filesystem.Stat(destFilePath)
    86  		if err != nil {
    87  			logger.Error(err)
    88  			panic(err)
    89  		}
    90  
    91  		if fsentry2.Type == fs.FileEntry {
    92  			fmt.Printf("Successfully uploaded a file %s to %s, size = %d\n", srcPath, destFilePath, fsentry2.Size)
    93  		} else {
    94  			logger.Errorf("Unkonwn file type - %s", fsentry2.Type)
    95  		}
    96  	}
    97  }